I'm now back from a fantastic break in Italy and am trying hard to catch back up. Next week I'm off again to San Diego (work, this time), which may cause further interruptions in blog postings.
This question came through from Genésio from Brazil:
I wish jig a leader with an bubble in the and of the leader, at the same time. Can you help me. Perhaps post the solution in your blog (through the interface).
It took me a while - frustratingly long, in fact, and probably this is not exactly what Genésio is after - but here's what I managed to come up with. The "bubble" is framed MText, but it should be modifiable to use a classic bubble block, instead. I drew heavily on this previous post for the jig code.
The positioning of the text took some work, but I'm reasonably happy with the results. If anyone has tweaks to suggest, please post a comment.
Here's the C# code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
namespace DimensionLibrary
{
public class DimensionCmds
{
class MLeaderJig : EntityJig
{
Point3dCollection m_pts;
Point3d m_tempPoint;
string m_contents;
int m_leaderIndex;
int m_leaderLineIndex;
public MLeaderJig(string contents)
: base(new MLeader())
{
// Store the string passed in
m_contents = contents;
// Create a point collection to store our vertices
m_pts = new Point3dCollection();
// Create mleader and set defaults
MLeader ml = Entity as MLeader;
ml.SetDatabaseDefaults();
// Set up the MText contents
ml.ContentType = ContentType.MTextContent;
MText mt = new MText();
mt.SetDatabaseDefaults();
mt.Contents = m_contents;
ml.MText = mt;
ml.TextAlignmentType =
TextAlignmentType.LeftAlignment;
ml.TextAttachmentType =
TextAttachmentType.AttachmentMiddle;
// Set the frame and landing properties
ml.EnableDogleg = true;
ml.EnableFrameText = true;
ml.EnableLanding = true;
// Reduce the standard landing gap
ml.LandingGap = 0.05;
// Add a leader, but not a leader line (for now)
m_leaderIndex = ml.AddLeader();
m_leaderLineIndex = -1;
}
protected override SamplerStatus Sampler(
JigPrompts prompts
)
{
JigPromptPointOptions opts =
new JigPromptPointOptions();
// Not all options accept null response
opts.UserInputControls =
(UserInputControls.Accept3dCoordinates |
UserInputControls.NoNegativeResponseAccepted
);
// Get the first point
if (m_pts.Count == 0)
{
opts.UserInputControls |=
UserInputControls.NullResponseAccepted;
opts.Message =
"\nStart point of multileader: ";
opts.UseBasePoint = false;
}
// And the second
else if (m_pts.Count == 1)
{
opts.BasePoint = m_pts[m_pts.Count - 1];
opts.UseBasePoint = true;
opts.Message =
"\nSpecify multileader vertex: ";
}
// And subsequent points
else if (m_pts.Count > 1)
{
opts.UserInputControls |=
UserInputControls.NullResponseAccepted;
opts.BasePoint = m_pts[m_pts.Count - 1];
opts.UseBasePoint = true;
opts.SetMessageAndKeywords(
"\nSpecify multileader vertex or [End]: ",
"End"
);
}
else // Should never happen
return SamplerStatus.Cancel;
PromptPointResult res =
prompts.AcquirePoint(opts);
if (res.Status == PromptStatus.Keyword)
{
if (res.StringResult == "End")
{
return SamplerStatus.Cancel;
}
}
if (m_tempPoint == res.Value)
{
return SamplerStatus.NoChange;
}
else if (res.Status == PromptStatus.OK)
{
m_tempPoint = res.Value;
return SamplerStatus.OK;
}
return SamplerStatus.Cancel;
}
protected override bool Update()
{
try
{
if (m_pts.Count > 0)
{
// Set the last vertex to the new value
MLeader ml = Entity as MLeader;
ml.SetLastVertex(
m_leaderLineIndex,
m_tempPoint
);
// Adjust the text location
Vector3d dogvec =
ml.GetDogleg(m_leaderIndex);
double doglen =
ml.DoglegLength;
double landgap =
ml.LandingGap;
ml.TextLocation =
m_tempPoint +
((doglen + landgap) * dogvec);
}
}
catch (System.Exception ex)
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
doc.Editor.WriteMessage(
"\nException: " + ex.Message
);
return false;
}
return true;
}
public void AddVertex()
{
MLeader ml = Entity as MLeader;
// For the first point...
if (m_pts.Count == 0)
{
// Add a leader line
m_leaderLineIndex =
ml.AddLeaderLine(m_leaderIndex);
// And a start vertex
ml.AddFirstVertex(
m_leaderLineIndex,
m_tempPoint
);
// Add a second vertex that will be set
// within the jig
ml.AddLastVertex(
m_leaderLineIndex,
new Point3d(0, 0, 0)
);
}
else
{
// For subsequent points,
// just add a vertex
ml.AddLastVertex(
m_leaderLineIndex,
m_tempPoint
);
}
// Reset the attachment point, otherwise
// it seems to get forgotten
ml.TextAttachmentType =
TextAttachmentType.AttachmentMiddle;
// Add the latest point to our history
m_pts.Add(m_tempPoint);
}
public void RemoveLastVertex()
{
// We don't need to actually remove
// the vertex, just reset it
MLeader ml = Entity as MLeader;
if (m_pts.Count >= 1)
{
Vector3d dogvec =
ml.GetDogleg(m_leaderIndex);
double doglen =
ml.DoglegLength;
double landgap =
ml.LandingGap;
ml.TextLocation =
m_pts[m_pts.Count - 1] +
((doglen + landgap) * dogvec);
}
}
public Entity GetEntity()
{
return Entity;
}
}
[CommandMethod("MYML")]
public void MyMLeaderJig()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// Get the text outside of the jig
PromptStringOptions pso =
new PromptStringOptions(
"\nEnter text: "
);
pso.AllowSpaces = true;
PromptResult pr =
ed.GetString(pso);
if (pr.Status == PromptStatus.OK)
{
// Create MleaderJig
MLeaderJig jig =
new MLeaderJig(pr.StringResult);
// Loop to set vertices
bool bSuccess = true, bComplete = false;
while (bSuccess && !bComplete)
{
PromptResult dragres = ed.Drag(jig);
bSuccess =
(dragres.Status == PromptStatus.OK);
if (bSuccess)
jig.AddVertex();
bComplete =
(dragres.Status == PromptStatus.None);
if (bComplete)
jig.RemoveLastVertex();
}
if (bComplete)
{
// Append entity
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead,
false
);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite,
false
);
btr.AppendEntity(jig.GetEntity());
tr.AddNewlyCreatedDBObject(
jig.GetEntity(),
true
);
tr.Commit();
}
}
}
}
}
}
Here's what you get when you run the MYML command:


Subscribe via RSS
Could you make a sample as the project 'BlockView' in ARX SDK using .net?
Posted by: zhutao | September 24, 2007 at 02:55 AM
Could you Creat a multileader in AutoCAD using a jig from 3D point to another.
for ex (23,43,566) to (133,455,988)
Posted by: frank | October 07, 2007 at 11:19 AM
This is really two questions, IMO:
1) Can multileaders support 3D vertices?
I haven't tried it myself, but the function signature does accept 3D points, so I assume it's 3D-capable (rather than being planar). If it were planar, then you could still do it, as long as the plane it's on intersects both points you mentioned.
2) Can jigs support 3D input?
There's nothing to stop you defining a jig that understands point input from the user in 3D coordinates: the trick is to make it logical. The most logical way to accept 3D input is to project the point information in the jig into the current UCS. The "jig3d" sample on the ObjectARX SDK (under samples/graphics/jig3d) shows how to do this from C++ - it shouldn't be hard to map the technique to .NET.
As for arbitrary 3D mouse input... this is something that is just hard to do. Applications doing things like piping come across this a lot, and often solve it by allowing movement in one of several constrained directions that can be selected by clicking on glyphs that indicate the direction you want to go in. Design Review does much the same when sectioning 3D views.
The other option is to just enter the coordinates via the keyboard. Which may very well just work with this jig, thinking about it.
Regards,
Kean
Posted by: Kean | October 07, 2007 at 12:35 PM
How can I retrieve the contents (i.e value) of a text entity with .net after adding the entity to a selection set.
Posted by: eddie | March 28, 2008 at 11:21 PM
You can open the entity and check its text property. This post shows how to iterate through a selection set. You will simply need to check whether it's a text entity and check the relevant property.
Kean
Posted by: Kean | March 29, 2008 at 09:41 AM