| McNeel Wiki | |||||
| edit · print · help · all topics | |||||
Main Pages
Languages
| QuestionWhat is the best approach to create a leader line? I have tried a couple of different approaches, but with no success. AnswerLeaders in Rhino 4.0 are defined by the ON_Leader2 class. To construct a leader, you must provide:
In the following example code, we will construct a simple leader object. The leader will reside in the world x-y plane and will have four points. C++
CRhinoCommand::result CCommandLeader::RunCommand( const CRhinoCommandContext& context )
{
// Some set of points that define the leader
ON_3dPointArray points;
points.Append( ON_3dPoint(1.0, 1.0, 0.0) );
points.Append( ON_3dPoint(5.0, 1.0, 0.0) );
points.Append( ON_3dPoint(5.0, 5.0, 0.0) );
points.Append( ON_3dPoint(9.0, 5.0, 0.0) );
// The plane in which the leader resides
ON_Plane plane = ON_xy_plane;
// Create the leader
ON_Leader2 leader;
leader.SetPlane( plane );
// Add the points to the leader
int i;
for( i = 0; i < points.Count(); i++ )
{
// Make sure the points are on the plane
ON_2dPoint p2;
if( leader.m_plane.ClosestPointTo(points[i], &p2.x, &p2.y) )
{
if( leader.m_points.Count() < 1 || p2.DistanceTo(*leader.m_points.Last()) > ON_SQRT_EPSILON )
leader.m_points.Append( p2 );
}
}
// Create the leader object
CRhinoAnnotationLeader* leader_object = new CRhinoAnnotationLeader();
// Add our leader to the object
leader_object->SetAnnotation( leader );
if( context.m_doc.AddObject(leader_object) )
context.m_doc.Redraw();
else
delete leader_object; // error
return success;
}
C# (Rhino 4)
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
// Some set of points that define the leader
On3dPointArray points = new On3dPointArray();
points.Append( 1.0, 1.0, 0.0);
points.Append( 5.0, 1.0, 0.0);
points.Append( 5.0, 5.0, 0.0);
points.Append( 9.0, 5.0, 0.0);
// Create the leader
OnLeader2 leader = new OnLeader2();
// Set the plane in which the leader resides
leader.SetPlane( OnUtil.On_xy_plane );
// Add the points to the leader
for( int i = 0; i < points.Count(); i++ )
{
// Make sure the points are on the plane
double x = 0;
double y = 0;
if( leader.m_plane.ClosestPointTo(points[i], ref x, ref y) )
{
On2dPoint p2 = new On2dPoint(x, y);
if( leader.m_points.Count() < 1 ||
p2.DistanceTo(leader.m_points.Last()) > OnUtil.On_SQRT_EPSILON )
leader.m_points.Append( p2 );
}
}
// Create the leader object
MRhinoAnnotationLeader leader_object = new MRhinoAnnotationLeader();
// Add our leader to the object
leader_object.SetAnnotation( leader );
if( context.m_doc.AddObject(leader_object) )
context.m_doc.Redraw();
return IRhinoCommand.result.success;
}
VB.NET (Rhino 4)
Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext) _
As RMA.Rhino.IRhinoCommand.result
' Some set of points that define the leader
Dim points As New On3dPointArray()
points.Append(1.0, 1.0, 0.0)
points.Append(5.0, 1.0, 0.0)
points.Append(5.0, 5.0, 0.0)
points.Append(9.0, 5.0, 0.0)
' Create the leader
Dim leader As New OnLeader2()
' Set the plane in which the leader resides
leader.SetPlane(OnUtil.On_xy_plane)
' Add the points to the leader
For i As Integer = 0 To points.Count() - 1
' Make sure the points are on the plane
Dim x As Double = 0
Dim y As Double = 0
If (leader.m_plane.ClosestPointTo(points(i), x, y)) Then
Dim p2 As New On2dPoint(x, y)
If (leader.m_points.Count() < 1 OrElse _
p2.DistanceTo(leader.m_points.Last()) > OnUtil.On_SQRT_EPSILON) Then
leader.m_points.Append(p2)
End If
End If
Next
' Create the leader object
Dim leader_object As New MRhinoAnnotationLeader()
' Add our leader to the object
leader_object.SetAnnotation(leader)
If (context.m_doc.AddObject(leader_object)) Then
context.m_doc.Redraw()
End If
Return IRhinoCommand.result.success
End Function
| ||||
| rename · changes · history · subscriptions · lost and found · references · file upload | |||||