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 attrNameValues = new Dictionary();
        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;
    }
    /// 
    /// 向模型空间插入块参照
    /// 
    /// 图形数据库
    /// 块的ObjectId
    /// 插入位置
    /// 旋转角度
    /// 缩放比例
    /// ObjectId
    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;
    }
    /// 
    /// 向模型空间插入属性块参照
    /// 
    /// 图形数据库
    /// 块的ObjectId
    /// 插入位置
    /// 旋转角度
    /// 缩放比例
    /// ObjectId
    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;
    }
    /// 
    /// 更新块参照的属性
    /// 
    /// 块参照的ObjectId
    /// 属性字典
    public static void UpdateBlockAttr(this ObjectId BlockRefId, Dictionary 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();
        }
    }
}

}

你可能感兴趣的:(c#,linq,开发语言)