CAD带属性快
生成高程点
输出 dxf 格式
使用 netDxf 开源库
netDxf
// 已有块(圆),无属性
const string gcd200File = @"D:\Data\CAD\块\GC200.dxf";
var dxf = new DxfDocument(DxfVersion.AutoCad2004);
var block = Block.Load(gcd200File, "GC200");
dxf.Blocks.Add(block);
var pos = new Vector3(1);
// 隐藏
var def = new AttributeDefinition("height")
{
Value = "0.00",
Color = AciColor.Blue,
Position = pos,
Alignment = TextAlignment.MiddleLeft,
IsVisible = false
};
block.AttributeDefinitions.Add(def);
var insert = new Insert(block, pos);
insert.TransformAttributes();
insert.Attributes[0].Value = "180.0";
insert.Attributes[0].IsVisible = true;
dxf.AddEntity(insert);
const string dxfFile = @"D:\Data\CAD\dxf\pointGC200.dxf";
dxf.Save(dxfFile);
MessageBox.Show("Done");
dwg CAD加载命令
ObjectArx .Net 2014
[CommandMethod("CMD1")]
public void Cmd1()
{
// 创建块
var blockName = "GCD2000";
var db = BaseApplication.DocumentManager.MdiActiveDocument.Database;
using (var trans = db.TransactionManager.StartTransaction())
{
var blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
var mdlSpc =
trans.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var gcdBlkId = ObjectId.Null;
// 块
if (!blkTbl.Has(blockName))
{
using (var gcdBlkDef = new BlockTableRecord())
{
gcdBlkDef.Name = blockName;
gcdBlkDef.Origin = Point3d.Origin;
// 圆
using (var circle = new Polyline())
{
circle.AddVertexAt(circle.NumberOfVertices,
Point2d.Origin + Vector2d.XAxis * -0.1,
Math.Tan(0.25 * Math.PI), 0.2, 0.2);
circle.AddVertexAt(circle.NumberOfVertices,
Point2d.Origin + Vector2d.XAxis * 0.1,
Math.Tan(0.25 * Math.PI), 0.2, 0.2);
circle.Closed = true;
gcdBlkDef.AppendEntity(circle);
// 属性
using (var attDef = new AttributeDefinition(Point3d.Origin, "0.00", "height", "输入 height",
ObjectId.Null))
{
attDef.Justify = AttachmentPoint.MiddleRight;
gcdBlkDef.AppendEntity(attDef);
blkTbl.Add(gcdBlkDef);
trans.AddNewlyCreatedDBObject(gcdBlkDef, true);
}
}
gcdBlkId = gcdBlkDef.Id;
}
}
else
{
gcdBlkId = blkTbl[blockName];
}
// 写入
if (gcdBlkId != ObjectId.Null)
{
var pos = Point3d.Origin + Vector3d.YAxis;
// 获取属性定义
var gcdDef = trans.GetObject(gcdBlkId, OpenMode.ForRead) as BlockTableRecord;
using (var gcdRef = new BlockReference(pos, gcdBlkId))
{
mdlSpc.AppendEntity(gcdRef);
trans.AddNewlyCreatedDBObject(gcdRef, true);
if (gcdDef.HasAttributeDefinitions)
{
foreach (var objID in gcdDef)
{
var dbObj = trans.GetObject(objID, OpenMode.ForRead);
if (dbObj is AttributeDefinition att)
{
if (!att.Constant)
{
using (var acAttRef = new AttributeReference())
{
acAttRef.SetAttributeFromBlock(att, gcdRef.BlockTransform);
acAttRef.Position = att.Position.TransformBy(gcdRef.BlockTransform);
acAttRef.TextString = att.TextString;
gcdRef.AttributeCollection.AppendAttribute(acAttRef);
trans.AddNewlyCreatedDBObject(acAttRef, true);
}
}
att.UpgradeOpen();
att.IsMirroredInX = true;
}
}
}
}
}
trans.Commit();
}
}