C# 代码在autocad中创建图层,但不绘制线

C# 代码在autocad中创建图层,但不绘制线,c#,autocad,C#,Autocad,此代码无法正常工作。它不绘制直线和多段线,而是创建图层。我认为问题可能与交易有关,但我不确定,也不知道如何解决 此方法负责将参数发送到创建方法 public static void DrawingLines() { Document document = Application.DocumentManager.MdiActiveDocument; Database database = document.Database; // Verificando a existe

此代码无法正常工作。它不绘制直线和多段线,而是创建图层。我认为问题可能与交易有关,但我不确定,也不知道如何解决

此方法负责将参数发送到创建方法

public static void DrawingLines()
{
    Document document = Application.DocumentManager.MdiActiveDocument;
    Database database = document.Database;

    // Verificando a existencia dos Layers, caso não existam, são criados
    CreatingLayer("P04", 4);
    CreatingLayer("TEXTO", 3);
    CreatingLayer("P01", 1);

    // Start a transaction
    using (Transaction transactionManager = 
           database.TransactionManager.StartTransaction())
    {
        // Cria o triangulo da função
        var acPoly = new Polyline();
        acPoly.AddVertexAt(0, new Point2d(Variaveis.pt.X - 225, 
                           Variaveis.pt.Y - 175), 0, 0, 0);
        acPoly.AddVertexAt(1, new Point2d(Variaveis.pt.X + 225, 
                           Variaveis.pt.Y - 175), 0, 0, 0);
        acPoly.AddVertexAt(2, new Point2d(Variaveis.pt.X + 225, 
                           Variaveis.pt.Y - 55), 0, 0, 0);
        acPoly.AddVertexAt(3, new Point2d(Variaveis.pt.X - 225, 
                           Variaveis.pt.Y - 175), 0, 0, 0);
        acPoly.Layer = "P01";

        // Desenha a linha central cyan
        var acLine = new Line()
        {
            StartPoint = new Point3d(Variaveis.pt.X + 53, Variaveis.pt.Y - 
                                     270, 0),
            EndPoint = new Point3d(Variaveis.pt.X + 53, Variaveis.pt.Y - 
                                   513, 0)
        };

        acLine.Layer = "P04";

        // Add the new object to the block table record
        AddToModelSpace(acPoly);
        AddToModelSpace(acLine);
    }
}
此方法负责检查层是否存在。如果它不存在,它将创建

public static void CreatingLayer(string layer, short color)
{
    Document document = Application.DocumentManager.MdiActiveDocument;
    Database database = document.Database;

    using (Transaction tr = database.TransactionManager.StartTransaction())
    {
        // Get the layer table from the drawing
        LayerTable lt = (LayerTable)tr.GetObject(database.LayerTableId, 
                         OpenMode.ForRead);

        // Create our new layer table record...
        LayerTableRecord ltr = new LayerTableRecord();

        // Checa a existencia de P01, caso não exista, é criada
        if (lt.Has(layer) == true)
        {
            database.Clayer = lt[layer];
        }
        else
        {
            // ... and set its properties
            ltr.Name = layer;
            ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, color);

            // Add the new layer to the layer table
            lt.UpgradeOpen();
            ObjectId ltId = lt.Add(ltr);
            tr.AddNewlyCreatedDBObject(ltr, true);

            // Set the layer to be current for this drawing
            database.Clayer = lt[layer];
        }

        // Se tudo ocorreu bem, grava a transação
        tr.Commit();
    }
}
此方法仅负责在模型空间中插入实体

public static void AddToModelSpace(Entity ent)
{
    Document document = Application.DocumentManager.MdiActiveDocument;
    Database database = document.Database;

    using (Transaction tr = database.TransactionManager.StartTransaction())
    {
        var blTbl = tr.GetObject(database.BlockTableId, OpenMode.ForRead) as 
                                 BlockTable;
        var blTblRec = tr.GetObject(blTbl[BlockTableRecord.ModelSpace], 
                                    OpenMode.ForWrite) as BlockTableRecord;

        blTblRec.AppendEntity(ent);
        tr.AddNewlyCreatedDBObject(ent, true);
        tr.Commit();
    }
}

我从未在AutoCAD环境中编写过任何代码,但检查您的代码时,您似乎没有在
绘图线中提交事务


这引出了一个有趣的观点;为什么您甚至要在
DrawingLines
中创建一个事务,然后立即在
AddToModelSpace
中创建另一个事务?其中一个似乎完全没有必要。

@erik philips现在我的代码工作得很好。多谢各位@erik philips我是编程新手。再次感谢你!