.NET

.NET

Reply
Active Member
10 Posts
0 Kudos
Registered: ‎05-24-2013
Post 1 of 5
Accepted Solution

How can I use Mline Class to draw mline in dwg using C#?

241 Views, 4 Replies
05-26-2013 06:52 PM

hello,

I want to draw mline in the dwg automatically. Here is some codes, and what should I do next?

 

C# codes by VS2010,FOR AUTOCAD 2010.

 

 Line dashdot = new Line();   

 dashdot.Linetype = "DashDot";

 MlineStyleElement off1100 = new MlineStyleElement(3720/2,Autodesk.AutoCAD.Colors.Color.FromRgb (0,0,0),dashdot .LinetypeId );  

 MlineStyle mlineStyle = new MlineStyle();

  mlineStyle.Elements.Add(off1100,false );

Mline mline = new Mline(); 

mline.Style = mlineStyle.ObjectId;//HOW CAN I DEFINE THE MLINE? it should contain lines and I don't know

                                                      // how to add lines(may be points) into it.

 

The code may be wrong.

Please give me some help and advice.

Thanks in advance.

Allen

Just a quick shot sorry,

without explanations

see docs for more

 [CommandMethod ("mil")]
public void MLINECREATE()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
Mline ml = new Mline();
ml.Style = db.CmlstyleID;
ml.Normal = Vector3d.ZAxis;
ml.Justification = MlineJustification.Zero;
ml.AppendSegment(new Point3d (0,0,0));
ml.AppendSegment(new Point3d(100, 0, 0));
ml.AppendSegment(new Point3d(100, 80, 0));
ml.AppendSegment(new Point3d(0, 80, 0));
ml.IsClosed = true;
btr.AppendEntity(ml);
tr.AddNewlyCreatedDBObject(ml, true);
tr.Commit();
}
doc.SendStringToExecute("_ZOOM _Ob _L ",true,false,false);
}

 

Found this code somewhere on forums

no tested, see if this helps

[CommandMethod("createmlinestyle")]
public void createmlinestyle()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor editor = doc.Editor;
Database db = doc.Database;
using (Transaction Tx =
db.TransactionManager.StartTransaction())
{
DBDictionary mlineDic =
(DBDictionary)Tx.GetObject(db.MLStyleDictionaryId,
OpenMode.ForRead);
if (!mlineDic.Contains("TEST"))
{
mlineDic.UpgradeOpen();
MlineStyle mlineStyle = new MlineStyle();
mlineDic.SetAt("TEST", mlineStyle);
Tx.AddNewlyCreatedDBObject(mlineStyle, true);
mlineStyle.EndAngle = 3.14159 * 0.5;
mlineStyle.StartAngle = 3.14159 * 0.5;
mlineStyle.Name = "TEST";
Autodesk.AutoCAD.Colors.Color Color;
Color =
Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);
MlineStyleElement element =
new MlineStyleElement(0.25,
Color,
db.Celtype);
mlineStyle.Elements.Add(element, true);
element =
new MlineStyleElement(-0.25,
Color,
db.Celtype);
mlineStyle.Elements.Add(element, false);
}
Tx.Commit();
}
}

 

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 5

Re: How can I use Mline Class to draw mline in dwg using C#?

05-26-2013 10:13 PM in reply to: allentable

Just a quick shot sorry,

without explanations

see docs for more

        [CommandMethod ("mil")]
        public void MLINECREATE()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                Mline ml = new Mline();
                ml.Style = db.CmlstyleID;
                
                ml.Normal = Vector3d.ZAxis;
                ml.Justification = MlineJustification.Zero;
                ml.AppendSegment(new Point3d (0,0,0));
                ml.AppendSegment(new Point3d(100, 0, 0));
                ml.AppendSegment(new Point3d(100, 80, 0));
                ml.AppendSegment(new Point3d(0, 80, 0));
                ml.IsClosed = true;
          
                btr.AppendEntity(ml);
                tr.AddNewlyCreatedDBObject(ml, true);
       
                tr.Commit();

            }
            doc.SendStringToExecute("_ZOOM _Ob _L  ",true,false,false);
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
10 Posts
0 Kudos
Registered: ‎05-24-2013
Post 3 of 5

Re: How can I use Mline Class to draw mline in dwg using C#?

05-26-2013 11:29 PM in reply to: Hallex

hi                   ,

Thank you for your so quick answer.

Your codes works well.

But can you give more codes about custom MlineStyle, my codes belows are wrong.

 

 Line dashdot = new Line();  

 dashdot.Linetype = "Dash";    

 MlineStyleElement off1100 = new MlineStyleElement(3720 / 2, Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 0), dashdot.LinetypeId);          

MlineStyle mlineStyle = new MlineStyle();         

 mlineStyle.Elements.Add(off1100, false);            

 Mline ml = new Mline();

 ml.Style = mlineStyle.ObjectId;

 

How can I use MlineStyle?

Thanks,

Allen

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 4 of 5

Re: How can I use Mline Class to draw mline in dwg using C#?

05-27-2013 01:19 AM in reply to: allentable

Found this code somewhere on forums

no tested, see if this helps

[CommandMethod("createmlinestyle")]
 
public void createmlinestyle()
 
{
 
    Document doc = Application.DocumentManager.MdiActiveDocument;
 
    Editor editor = doc.Editor;
 
    Database db = doc.Database;
 
    using (Transaction Tx =
 
        db.TransactionManager.StartTransaction())
 
    {
 
 
 
        DBDictionary mlineDic = 

            (DBDictionary)Tx.GetObject(db.MLStyleDictionaryId, 

                                              OpenMode.ForRead);
 
        if (!mlineDic.Contains("TEST"))
 
        {
 
            mlineDic.UpgradeOpen();
 
            MlineStyle mlineStyle = new MlineStyle();
 
            mlineDic.SetAt("TEST", mlineStyle);
 
            Tx.AddNewlyCreatedDBObject(mlineStyle, true);
 
 
 
            mlineStyle.EndAngle = 3.14159 * 0.5;
 
            mlineStyle.StartAngle = 3.14159 * 0.5;
 
            mlineStyle.Name = "TEST";
 
            Autodesk.AutoCAD.Colors.Color Color;
 
            Color = 

                Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0);
 
 
 
            MlineStyleElement element = 

                    new MlineStyleElement(0.25, 

                                        Color, 

                                         db.Celtype);
 
            mlineStyle.Elements.Add(element, true);
 
            element = 

                    new MlineStyleElement(-0.25, 

                                        Color, 

                                        db.Celtype);
 
 
 
            mlineStyle.Elements.Add(element, false);
 
        }
 
 
 
        Tx.Commit();
 
    }
 
}

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
10 Posts
0 Kudos
Registered: ‎05-24-2013
Post 5 of 5

Re: How can I use Mline Class to draw mline in dwg using C#?

05-27-2013 05:14 AM in reply to: Hallex

@Hallex,

Thanks very much.

I get what I want.

 

Best regards,:smileyhappy:

Allen

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!