此代码无法正常运行。它不绘制直线和折线,而是创建图层。我认为问题可能与交易有关,但我不确定,我也不知道如何解决。
此方法负责将参数发送到创建方法。
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();
}
}
答案 0 :(得分:2)
我从来没有在AutoCAD环境中编写任何代码,但检查代码时似乎没有在DrawingLines
中提交事务。
这带来了一个有趣的观点;为什么你甚至在DrawingLines
创建一个交易并立即在AddToModelSpace
创建另一个交易?其中一个似乎完全没必要。