.NET

Reply
Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 1 of 3 (191 Views)
Accepted Solution

moving Z coordinate on MLeader objects

191 Views, 2 Replies
03-04-2014 11:29 AM

Hi, I'm hoping somebody can help me out here. I have some multileader objects that have a Z-coordinate that I need to put to 0. I have been successfull with Mtext and Dtext, but i'm struggling with the multileader. This is what I have so far. Don't get any errors, but it does not move to a Z value of 0.

 

If myPSR1.Status = PromptStatus.OK Then
                    acSSet = myPSR1.Value
                    Dim trans1 As Transaction = myDocument.TransactionManager.StartTransaction
                    Try
                        For Each selObj In acSSet
                            Select Case selObj.ObjectId.ObjectClass.Name
                                Case "AcDbText"
                                    Dim textObj As DBText = trans1.GetObject(selObj.ObjectId, OpenMode.ForWrite, False, True)
                                    OrigX = textObj.Position.X
                                    OrigY = textObj.Position.Y

                                    Newpt = New Point3d(OrigX, OrigY, 0)
                                    textObj.Position = Newpt

                                Case "AcDbMText"
                                    Dim mtextObj As MText = trans1.GetObject(selObj.ObjectId, OpenMode.ForWrite, False, True)
                                    OrigX = mtextObj.Location.X
                                    OrigY = mtextObj.Location.Y

                                    Newpt = New Point3d(OrigX, OrigY, 0)
                                    mtextObj.Location = Newpt

                                Case "AcDbMLeader"
                                    Dim mtextObj As MLeader = trans1.GetObject(selObj.ObjectId, OpenMode.ForWrite, False, True)
                                    Dim basePoint As Point3d
                                    Select Case mtextObj.ContentType
                                        Case ContentType.BlockContent
                                            basePoint = mtextObj.BlockPosition
                                            OrigX = mtextObj.BlockPosition.X
                                            OrigY = mtextObj.BlockPosition.Y
                                            Newpt = New Point3d(OrigX, OrigY, 0)
                                            mtextObj.BlockPosition = Newpt

                                        Case ContentType.MTextContent
                                            basePoint = mtextObj.TextLocation
                                            OrigX = mtextObj.TextLocation.X
                                            OrigY = mtextObj.TextLocation.Y
                                            Newpt = New Point3d(OrigX, OrigY, 0)
                                            mtextObj.TextLocation = Newpt
                                    End Select

                            End Select
                        Next selObj
                        ' all ok, commit it
                        trans1.Commit()
                    Catch ex As Autodesk.AutoCAD.Runtime.Exception
                        MsgBox("A problem occured because " + ex.Message)
                    Finally
                        ' whatever happens we must dispose the transaction
                        trans1.Dispose()
                    End Try
                End If

 Any help or guidance would be greatly appreciated.

 

thanks

 

cj

 

Hi,

 

You can try this way:

 

 [CommandMethod("Test", CommandFlags.Modal)]
public void Test()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
TypedValue[] values = { new TypedValue(0, "TEXT,MTEXT,MULTILEADER") };
PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(values));
if (psr.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId id in psr.Value.GetObjectIds())
{
switch (id.ObjectClass.Name)
{
case "AcDbText":
DBText text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
Flatten(text, text.Position);
break;
case "AcDbMText":
MText mtext = (MText)tr.GetObject(id, OpenMode.ForWrite);
Flatten(mtext, mtext.Location);
break;
default: // MLeader
MLeader mleader = (MLeader)tr.GetObject(id, OpenMode.ForWrite);
Flatten(mleader, mleader.GetVertex(0, 0));
break;
}
}
tr.Commit();
}
}
private void Flatten(Entity ent, Point3d basePt)
{
if (basePt.Z != 0.0)
ent.TransformBy(Matrix3d.Displacement(new Vector3d(0.0, 0.0, -basePt.Z)));
}

 

*Expert Elite*
_gile
Posts: 2,093
Registered: ‎04-29-2006
Message 2 of 3 (179 Views)

Re : moving Z coordinate on MLeader objects

03-04-2014 01:08 PM in reply to: cjacobi

Hi,

 

You can try this way:

 

        [CommandMethod("Test", CommandFlags.Modal)]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            TypedValue[] values = { new TypedValue(0, "TEXT,MTEXT,MULTILEADER") };
            PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(values));
            if (psr.Status != PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id  in psr.Value.GetObjectIds())
                {
                    switch (id.ObjectClass.Name)
                    {
                        case "AcDbText":
                            DBText text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
                            Flatten(text, text.Position);
                            break;
                        case "AcDbMText":
                            MText mtext = (MText)tr.GetObject(id, OpenMode.ForWrite);
                            Flatten(mtext, mtext.Location);
                            break;
                        default: // MLeader
                            MLeader mleader = (MLeader)tr.GetObject(id, OpenMode.ForWrite);
                            Flatten(mleader, mleader.GetVertex(0, 0));
                            break;
                    }
                }
                tr.Commit();
            }
        }

        private void Flatten(Entity ent, Point3d basePt)
        {
            if (basePt.Z != 0.0)
                ent.TransformBy(Matrix3d.Displacement(new Vector3d(0.0, 0.0, -basePt.Z)));
        }

 

Gilles Chanteau
Valued Contributor
cjacobi
Posts: 54
Registered: ‎11-12-2012
Message 3 of 3 (170 Views)

Re : moving Z coordinate on MLeader objects

03-04-2014 01:47 PM in reply to: _gile

THANKS Gilles!!

 

I learned a ton from your quick response and got my code working the way I want it to.

 

cheers

 

cj

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.