c# cad 二次开发 类库 块的操作

c# cad 二次开发 类库 块的操作
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14块操作
{
public class Class1
{
//简单块
[CommandMethod(“BlockDemo”)]
public void BlockDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
//using (Transaction trans = db.TransactionManager.StartTransaction())
//{
// BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
// foreach (var item in bt)
// {
// BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);
// }
//}
MyBlockTableRecord.Block1Id = db.AddBlockTableRecord(MyBlockTableRecord.Block1Name, MyBlockTableRecord.Block1Ents);

    }

    [CommandMethod("InsertBlockDemo")]
    public void InsertBlockDemo()
    {
        Database db = HostApplicationServices.WorkingDatabase;
        db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(10, 10, 0));
        db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(40, 10, 0),Math.PI/4,new Scale3d(2));
        db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(100, 10, 0), Math.PI / 4, new Scale3d(2,1.5,1));
    }

    //注释样式的箭头
    [CommandMethod("ModifyDimDemo")]
    public void ModifyDimDemo()
    {
        Database db = HostApplicationServices.WorkingDatabase;
        MyBlockTableRecord.DimBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.DimBlock1Name, MyBlockTableRecord.DimBlock1Ents);
        using (Transaction trans = db.TransactionManager.StartTransaction())
        {
            
            DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);
            if (dst.Has("Standard"))
            {
                DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);
                if (MyBlockTableRecord.DimBlock1Id != ObjectId.Null)
                {
                    dstr.UpgradeOpen();
                    dstr.Dimblk = MyBlockTableRecord.DimBlock1Id;
                    dstr.Dimasz = 1;
                    dstr.DowngradeOpen();
                    db.SetDimstyleData(dstr);
                }
            }
            trans.Commit();
        }
    }

    //属性快
    [CommandMethod("AttrBlockDemo")]
    public void AttrBlockDemo()
    {
        Database db = HostApplicationServices.WorkingDatabase;
        MyBlockTableRecord.AttrBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.AttrBlock1Name, MyBlockTableRecord.AttrBlock1Ents);
        ObjectId brId = db.InsertAttrBlockReference(MyBlockTableRecord.AttrBlock1Id, new Point3d(10, 10, 0), 0, new Scale3d(1, 1, 1));

        Dictionary<string, string> attrNameValues = new Dictionary<string, string>();
        attrNameValues.Add("编号", "天线11");
        attrNameValues.Add("功率", "10.0dBm");
        attrNameValues.Add("功率222", "10.0dBm");
        brId.UpdateBlockAttr(attrNameValues);

    }


    [CommandMethod("TestDemo")]
    public void TestDemo()
    {
        Database db = HostApplicationServices.WorkingDatabase;
        MyBlockTableRecord.AttrBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.AttrBlock1Name, MyBlockTableRecord.AttrBlock1Ents);
        using (Transaction trans = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            //DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);
            //if (dst.Has("Standard"))
            //{
            //    DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);
            //}
            foreach (var item in bt)
            {
                if (bt.Has(MyBlockTableRecord.AttrBlock1Id))
                {
                    if (item  == MyBlockTableRecord.AttrBlock1Id)
                    {
                        BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);

                        foreach (var item1  in btr)
                        {
                            if (item1.GetObject(OpenMode.ForRead) is AttributeDefinition)
                            {
                                AttributeDefinition attr = (AttributeDefinition)item1.GetObject(OpenMode.ForRead);
                            }
                            
                        }
                    }  
                }
                
            }
        }
    }

    [CommandMethod("PickDemo")]
    public void PickDemo()
    {
        Database db = HostApplicationServices.WorkingDatabase;
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        PromptEntityResult per = ed.GetEntity("选择块");
        if (per.Status == PromptStatus.OK)
        {
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockReference br = (BlockReference)per.ObjectId.GetObject(OpenMode.ForRead);
            } 
        }
    }

}
}
BlockTool.cs

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14块操作
{
public static class BlockTool
{
///
/// 添加块表记录到图形数据库
///
/// 图形数据库
/// 块表记录名
/// 图形对象
/// ObjectId
public static ObjectId AddBlockTableRecord(this Database db, string btrName, List ents)
{
ObjectId btrId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(btrName))
{
BlockTableRecord btr = new BlockTableRecord();
btr.Name = btrName;
for (int i = 0; i < ents.Count; i++)
{
btr.AppendEntity(ents[i]);
}
bt.UpgradeOpen();
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
bt.DowngradeOpen();
}
btrId = bt[btrName];
trans.Commit();
}
return btrId;
}
///
/// 向模型空间插入块参照
///
/// 图形数据库
/// 块的ObjectId
/// 插入位置
/// ObjectId
public static ObjectId InsertBlockBlockReference(this Database db, ObjectId blockRecordId, Point3d position)
{
ObjectId blkReferId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(blockRecordId))
{
//声明块参照
BlockReference br = new BlockReference(position, blockRecordId);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
blkReferId = btr.AppendEntity(br);
trans.AddNewlyCreatedDBObject(br, true);
}
trans.Commit();
}

        return blkReferId;
    }
    /// <summary>
    /// 向模型空间插入块参照
    /// </summary>
    /// <param name="db">图形数据库</param>
    /// <param name="blockRecordId">块的ObjectId</param>
    /// <param name="position">插入位置</param>
    /// <param name="rotation">旋转角度</param>
    /// <param name="scale">缩放比例</param>
    /// <returns>ObjectId</returns>
    public static ObjectId InsertBlockBlockReference(this Database db, ObjectId blockRecordId, Point3d position,double rotation,Scale3d scale)
    {
        ObjectId blkReferId = ObjectId.Null;
        using (Transaction trans = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            if (bt.Has(blockRecordId))
            {
                BlockReference br = new BlockReference(position, blockRecordId);
                br.Rotation = rotation;
                br.ScaleFactors = scale;
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                blkReferId = btr.AppendEntity(br); 
                trans.AddNewlyCreatedDBObject(br, true);
            }
            trans.Commit();
        }

        return blkReferId;
    }
    /// <summary>
    /// 向模型空间插入属性块参照
    /// </summary>
    /// <param name="db">图形数据库</param>
    /// <param name="blockRecordId">块的ObjectId</param>
    /// <param name="position">插入位置</param>
    /// <param name="rotation">旋转角度</param>
    /// <param name="scale">缩放比例</param>
    /// <returns>ObjectId</returns>
    public static ObjectId InsertAttrBlockReference(this Database db, ObjectId blockRecordId, Point3d position, double rotation, Scale3d scale)
    {
        ObjectId blkReferId = ObjectId.Null;
        using (Transaction trans = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            if (bt.Has(blockRecordId))
            {
                //声明块参照
                BlockReference br = new BlockReference(position, blockRecordId);
                br.Rotation = rotation;
                br.ScaleFactors = scale;
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                blkReferId = btr.AppendEntity(br);
                //添加属性定义
                BlockTableRecord blockRecord = (BlockTableRecord)blockRecordId.GetObject(OpenMode.ForRead);
                if (blockRecord.HasAttributeDefinitions)
                {
                    foreach (ObjectId item in blockRecord)
                    {
                        DBObject obj = item.GetObject(OpenMode.ForRead);
                        if (obj is AttributeDefinition)
                        {
                            //声明属性参照
                            AttributeReference attrRef = new AttributeReference();
                            attrRef.SetAttributeFromBlock((AttributeDefinition)obj, br.BlockTransform);
                            br.AttributeCollection.AppendAttribute(attrRef);
                            trans.AddNewlyCreatedDBObject(attrRef, true);
                        }
                    }
                }
                
                trans.AddNewlyCreatedDBObject(br, true);
            }
            trans.Commit();
        }

        return blkReferId;
    }
    /// <summary>
    /// 更新块参照的属性
    /// </summary>
    /// <param name="BlockRefId">块参照的ObjectId</param>
    /// <param name="attrNameValues">属性字典</param>
    public static void UpdateBlockAttr(this ObjectId BlockRefId, Dictionary<string, string> attrNameValues)
    {
        using (Transaction trans = BlockRefId.Database.TransactionManager.StartTransaction())
        {
            if (BlockRefId != ObjectId.Null)
            {
                BlockReference br = (BlockReference)BlockRefId.GetObject(OpenMode.ForRead);
                foreach (ObjectId item in br.AttributeCollection)
                {
                    AttributeReference attRef = (AttributeReference)item.GetObject(OpenMode.ForRead);
                    //判断属性字典中是否包含要更改的属性值
                    if (attrNameValues.ContainsKey(attRef.Tag.ToString()))
                    {
                        attRef.UpgradeOpen();
                        attRef.TextString = attrNameValues[attRef.Tag.ToString()].ToString();
                        attRef.DowngradeOpen();
                    }
                }
            }
            trans.Commit();
        }
    }
}

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DK业

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值