[原创]翔麟专集——实现类似CAD中的Block创建块义命令程序
public void CreateBlock()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
ObjectId blockId = ObjectId.Null; //用于返回所创建的块的对象Id
BlockTableRecord record = new BlockTableRecord(); //创建一个BlockTableRecord类的对象,表示所要创建的块
record.Name = "MyBlock"; //设置块名
record.Origin = new Point3d(0, 0, 0); //设置块的基点
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);//以写的方式打开块表
if (!bt.Has(record.Name))
{
//选择对象
PromptSelectionResult res = ed.GetSelection();
if (res.Status == PromptStatus.OK)
{
foreach (ObjectId id in res.Value.GetObjectIds())
{
Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
Entity NewEnt = (Entity)ent.Clone();
record.AppendEntity(NewEnt);
}
}
bt.Add(record); //在块表中加入块
trans.AddNewlyCreatedDBObject(record, true);//通知事务处理
}
else
{
ed.WriteMessage("此图块名已存在,请检查!");
}
trans.Commit();
}
}
}
以下是Vs2008-CAD2010程序源文件:
<p>很好.这个功能.我已经有了.看晚了点了.</p><p></p> Cool!! 前几天还在为此功能郁闷呢,不过现在狐哥的帮助下已解决了... 很不错。但有一个问题就是。创建块后,能不能把用来创建块的对像也一起转换成块呢。因为平时我们选择对象创建块后。选择的那些对象就会同时转换成块了。 回复 lubing 的帖子
把这几句
foreach (ObjectId id in res.Value.GetObjectIds())
{
Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
Entity NewEnt = (Entity)ent.Clone();
record.AppendEntity(NewEnt);
}
换成
record.AssumeOwnershipOf( new ObjectIdCollection(res.Value.GetObjectIds())); 学习了,不错 这个不能指定插入点 2012版测试无法通过,需要改为下面代码:using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace CollectionCreation
{
public class Commands
{
public void CreateBlock()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
// Get the block table from the drawing
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
// Check the block name, to see whether it's
// already in use
PromptStringOptions pso =
new PromptStringOptions(
"\nEnter new block name: "
);
pso.AllowSpaces = true;
// A variable for the block's name
string blkName = "";
do
{
PromptResult pr = ed.GetString(pso);
// Just return if the user cancelled
// (will abort the transaction as we drop out of the using
// statement's scope)
if (pr.Status != PromptStatus.OK)
return;
try
{
// Validate the provided symbol table name
SymbolUtilityServices.ValidateSymbolName(
pr.StringResult,
false
);
// Only set the block name if it isn't in use
if (bt.Has(pr.StringResult))
ed.WriteMessage(
"\nA block with this name already exists."
);
else
blkName = pr.StringResult;
}
catch
{
// An exception has been thrown, indicating the
// name is invalid
ed.WriteMessage(
"\nInvalid block name."
);
}
} while (blkName == "");
// Create our new block table record...
BlockTableRecord btr = new BlockTableRecord();
// ... and set its properties
btr.Name = blkName;
// Add the new block to the block table
bt.UpgradeOpen();
ObjectId btrId = bt.Add(btr);
tr.AddNewlyCreatedDBObject(btr, true);
// Add some lines to the block to form a square
// (the entities belong directly to the block)
DBObjectCollection ents = SquareOfLines(5);
foreach (Entity ent in ents)
{
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
// Add a block reference to the model space
BlockTableRecord ms =
(BlockTableRecord)tr.GetObject(
bt,
OpenMode.ForWrite
);
BlockReference br =
new BlockReference(Point3d.Origin, btrId);
ms.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
// Commit the transaction
tr.Commit();
// Report what we've done
ed.WriteMessage(
"\nCreated block named \"{0}\" containing {1} entities.",
blkName, ents.Count
);
}
}
private DBObjectCollection SquareOfLines(double size)
{
// A function to generate a set of entities for our block
DBObjectCollection ents = new DBObjectCollection();
Point3d[] pts =
{ new Point3d(-size, -size, 0),
new Point3d(size, -size, 0),
new Point3d(size, size, 0),
new Point3d(-size, size, 0)
};
int max = pts.GetUpperBound(0);
for (int i = 0; i <= max; i++)
{
int j = (i == max ? 0 : i + 1);
Line ln = new Line(pts, pts);
ents.Add(ln);
}
return ents;
}
}
}
这个是VBA吗
页:
[1]
2