Hello,
I want to use MlineStyle to custom mLine.
Here are my own codes and it does't work.
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 in advance.
Allen
[CommandMethod ("mil")] public void MLINECREATE() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); DBDictionary mlineDic =(DBDictionary)tr.GetObject(db.MLStyleDictionaryId,OpenMode.ForRead); if (!mlineDic.Contains("TEST")) { mlineDic.UpgradeOpen(); MlineStyle mlineStyle = new MlineStyle(); mlineDic.SetAt("TEST", mlineStyle); tr.AddNewlyCreatedDBObject(mlineStyle, true); mlineStyle.EndAngle = 3.14159 * 0.5; mlineStyle.StartAngle = 3.14159 * 0.5; mlineStyle.Name = "TEST"; MlineStyleElement off1100 = new MlineStyleElement(10 / 2, Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0), db.Celtype); mlineStyle.Elements.Add(off1100, true ); MlineStyleElement off0 = new MlineStyleElement(0, Autodesk.AutoCAD.Colors.Color.FromRgb(255, 255,255), db.Celtype); mlineStyle.Elements.Add(off0, true ); MlineStyleElement off1100n = new MlineStyleElement(-10 / 2, Autodesk.AutoCAD.Colors.Color.FromRgb(255,255, 0), db.Celtype); mlineStyle.Elements.Add(off1100n, true ); mlineStyle.ShowMiters = false; Mline ml = new Mline(); ml.Style =mlineStyle .ObjectId; //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 =false ; btr.AppendEntity(ml); tr.AddNewlyCreatedDBObject(ml, true); } tr.Commit(); } doc.SendStringToExecute("_ZOOM _Ob _L ",true,false,false); }
The code (mainly supplied by Hallex, Thanks again) can work , but when I open the menu [options]-[mlinestyle] and CLICK the "TEST" in the STYLE list,the autocad application occurs an error and closed. I don't know the reason.
Any help will be appreciated.
Allen
I've added small changes, this code working on my A2010,
see if this helps
[CommandMethod("mil")] public void MLINECREATE() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; using (Transaction tr = db.TransactionManager.StartTransaction()) { doc.TransactionManager.EnableGraphicsFlush(true); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); DBDictionary mlineDic = (DBDictionary)tr.GetObject(db.MLStyleDictionaryId, OpenMode.ForRead); if (!mlineDic.Contains("TEST")) { mlineDic.UpgradeOpen(); MlineStyle mlineStyle = new MlineStyle(); mlineDic.SetAt("TEST", mlineStyle); tr.AddNewlyCreatedDBObject(mlineStyle, true); mlineStyle.EndAngle = 3.14159 * 0.5; mlineStyle.StartAngle = 3.14159 * 0.5; mlineStyle.Name = "TEST"; MlineStyleElement off1100 = new MlineStyleElement(10 / 2, Autodesk.AutoCAD.Colors.Color.FromRgb(255, 0, 0), db.Celtype); mlineStyle.Elements.Add(off1100, true); MlineStyleElement off0 = new MlineStyleElement(0, Autodesk.AutoCAD.Colors.Color.FromRgb(255, 255, 255), db.Celtype); mlineStyle.Elements.Add(off0, true); MlineStyleElement off1100n = new MlineStyleElement(-10 / 2, Autodesk.AutoCAD.Colors.Color.FromRgb(255, 255, 0), db.Celtype); mlineStyle.Elements.Add(off1100n, true); mlineStyle.EndInnerArcs = false; mlineStyle.EndSquareCap=false; mlineStyle.EndRoundCap = false; mlineStyle.ShowMiters = false; db.CmlstyleID = mlineStyle.ObjectId; tr.TransactionManager.QueueForGraphicsFlush(); Mline ml = new Mline(); ml.Style = mlineStyle.ObjectId; //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.TransactionManager.QueueForGraphicsFlush(); } doc.TransactionManager.FlushGraphics(); tr.Commit(); } // doc.SendStringToExecute("_ZOOM _Ob _L ", true, false, false); }
Thanks, Hallex,
The codes really works, but when I open the menu [options]-[mlinestyle] to see the details.
The application shows errors again,
System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在 Autodesk.AutoCAD.DatabaseServices.DBObject.DeleteUnmanagedObject()
在 Autodesk.AutoCAD.Runtime.DisposableWrapper.!DisposableWrapper()
在 Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose(Boolean )
在 Autodesk.AutoCAD.DatabaseServices.DBObject.Dispose(Boolean )
在 Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose()
在 Autodesk.AutoCAD.AcMultiLineUi.MlineStylePreviewControl.set_PreviewStyle(MlineStyle value)
在 Autodesk.AutoCAD.AcMultiLineUi.MlineStyleForm.UpdateSelected()
在 Autodesk.AutoCAD.AcMultiLineUi.MlineStyleForm.MlineStyleForm_Load(Object sender, EventArgs e)
在 System.EventHandler.Invoke(Object sender, EventArgs e)
在 System.Windows.Forms.Form.OnLoad(EventArgs e)
在 System.Windows.Forms.Form.OnCreateControl()
在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
在 System.Windows.Forms.Control.CreateControl()
在 System.Windows.Forms.Control.WmShowWindow(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WmShowWindow(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
translate into English:
attempt to read from or write into protected memory,this usually indicates other memory is broken.
Maybe something is wrong with my application.
If I didn't try to open it, this can be the whole solution.
Thanks again.
Best regards.
Allen
Hi Allen,
I could reproduce the crash in AutoCAD 2013.
Please try including this line of code :
mlineStyle.Description = String.Empty;
It seems AutoCAD 2013 is more particular about setting this empty when there is no description text for the style.
Hi Balaji_Ram,
This mlineStyle.Description = String.Empty; really solve the problem.
Thanks