C# 通过COM接口实现导入外部块功能/创建块并插入块参照

private void btnBlock_Click(object sender, EventArgs e)
{

double[] insertionPoint = new double[3];
insertionPoint[0] = 0;
insertionPoint[1] = 0;
insertionPoint[2] = 0.0;
string progid = "ObjectDBX.AxDbDocument.22";
dbx.AxDbDocument dbxDoc;
dbxDoc = (dbx.AxDbDocument)m_acadApp.GetInterfaceObject(progid);
dbxDoc.Open("F:\\Testblock.dwg");//包含多个块的文件
AcadBlock block = (AcadBlock)dbxDoc.Blocks.Item("Testblock");//Testblock 该块只是文件中的其中一个,可以将其参数化
AcadObject[] obj = new AcadObject[1];
obj[0] = (AcadObject)block;
AcadExternalReference xrefInserted;
xrefInserted = dbxDoc.CopyObjects(obj, m_acadApp.ActiveDocument.Database.Blocks);//将块信息拷贝到当前块集合中
m_acadApp.ActiveDocument.ModelSpace.InsertBlock(insertionPoint, "Testblock", 1, 1, 1, 0);//插入到当前文件
m_acadApp.ZoomAll();
}

//创建块并插入块参照

double[] insertionPoint = new double[3];
insertionPoint[0] = 0;
insertionPoint[1] = 0;
insertionPoint[2] = 0.0;
AcadBlock destBlock = m_acadApp.ActiveDocument.Blocks.Add(insertionPoint, "TestBlock");
double[] startPoint = new double[3] { 0, 0, 0 };
double[] endPoint = new double[3] { 100, 100, 100 };
destBlock.AddLine(startPoint, endPoint);
m_acadApp.ActiveDocument.ModelSpace.AttachExternalReference(pathname, "TestBlock", insertionPoint, 1, 1, 1, 0, false);
m_acadApp.ZoomAll();
m_acadApp.Application.Update();

 

 

posted on 2022-04-07 13:28  会叫的鹦鹉  阅读(294)  评论(0编辑  收藏  举报