CAD带属性快(c#)

生成高程点

在这里插入图片描述

输出 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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值