模拟CAD中,框选实体对象制作块,并插入到模型空间
如题所示,想模拟cad中的block命令,类似 <燕秀工具箱> 里面的 创建 匿名块在cad中已经画好的图元,通过框选实体对象,制成块表然后插入块参照(普通块)不知道在cad内部是怎么样的一个操作流程,在我选择的实体中,插入到数据库中时,提示已经存在
网上到处也没有找到类似或者是没有找到搜的方式
求大神指点一下,C# 语言 谢谢 :handshake
本帖最后由 caiqs 于 2019-12-16 18:39 编辑
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
namespace MakeBlock
{
/// <summary>
/// Summary description for Commands.
/// </summary>
public class Commands
{
public Commands()
{
//
// TODO: Add constructor logic here
//
}
// Define Command "AsdkCmd1"
//师兄 QQ361865648,一直都在用VC,好长时间没写C#了
static public void test() // This method can have any name
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptSelectionResult ssres = ed.GetSelection();
if (ssres.Status == PromptStatus.OK)
{
Point3d basept = new Point3d(0, 0, 0);
PromptPointResult ptres = ed.GetPoint("\n指定基点:");
if (ptres.Status == PromptStatus.OK)
basept = ptres.Value;
if (ptres.Status == PromptStatus.Cancel) return;
Transaction tr = db.TransactionManager.StartTransaction();
ObjectIdCollection ids = new ObjectIdCollection(ssres.Value.GetObjectIds());
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord mspbtr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord btr = new BlockTableRecord();
btr.Name = "*U";
btr.Origin = basept;
ObjectId id = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
IdMapping idm = new IdMapping();
db.DeepCloneObjects(ids, id, idm, false);
BlockReference bref = new BlockReference(basept, id);
mspbtr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
//此处删除原实体
foreach (ObjectId Oldid in ids)
{
DBObject old = tr.GetObject(Oldid, OpenMode.ForWrite);
old.Erase();
}
tr.Commit();
}
// Put your command code here
}
}
}
页:
[1]