.Net AutoCAD Mechanical OperateTransaction.Commit不起作用

时间:2019-03-19 07:05:41

标签: .net autocad

我创建一个新图层,并希望用户选择一个将属于新图层的对象

Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

 using (Transaction OperateTransaction = acCurDb.TransactionManager.StartTransaction())
{
    using (LayerTable LayerList = OperateTransaction.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite) as LayerTable)
    {
        LayerTableRecord NewLayer = new LayerTableRecord();
        NewLayer.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Color.FromArgb(RandomNum.Next(255), RandomNum.Next(255), RandomNum.Next(255)));
        NewLayer.Name = NewLayerName;

        OperateTransaction.AddNewlyCreatedDBObject(NewLayer, true);
    }

    OperateTransaction.Commit();
}


PromptSelectionResult acSSPrompt = Application.DocumentManager.MdiActiveDocument.Editor.GetSelection();

 if (acSSPrompt.Status == PromptStatus.OK)
 {
     //... Assign object to new layer
 }

在AutoCAD 2013和AutoCAD 2013中它可以工作,当用户选择对象时,他可以在AutoCAD中看到新层,但是在AutoCAD Mechanical 2016中,除非我的.Net程序关闭,否则它不起作用。

1 个答案:

答案 0 :(得分:0)

我认为您忘记了将图层添加到图层表中:

using (LayerTable LayerList = OperateTransaction.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite) as LayerTable)
{
    LayerTableRecord NewLayer = new LayerTableRecord();
    NewLayer.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Color.FromArgb(RandomNum.Next(255), RandomNum.Next(255), RandomNum.Next(255)));
    NewLayer.Name = NewLayerName;

    //add this
    LayerList.Add(NewLayer)

    OperateTransaction.AddNewlyCreatedDBObject(NewLayer, true);
}