我正在尝试将DWG文件插入一个空模板。第一次插入DWG文件已完成,但是当用户尝试添加其他DWG文件时,我需要打开新模板并插入DWG。
这是插入功能,可以在打开空模板时使用:
public void InsertDrawingAsBlock(Document doc, string path, string blockname, Point3d ipt)
{
try
{
Database curdb = doc.Database;
Editor ed = doc.Editor;
DocumentLock loc = doc.LockDocument();
using (loc)
{
ObjectId blkid = ObjectId.Null;
Database db = new Database(false, true);
using (db)
{
db.ReadDwgFile(path, System.IO.FileShare.Read, true, "");
blkid = curdb.Insert(path, db, true);
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(curdb.BlockTableId, OpenMode.ForRead);
if (bt.Has(blockname))
{
System.Windows.Forms.MessageBox.Show(string.Format("Block {0} does already exist\nTry another block or Exit", blockname));
return;
}
bt.UpgradeOpen();
BlockTableRecord btrec = (BlockTableRecord)blkid.GetObject(OpenMode.ForRead);
btrec.UpgradeOpen();
btrec.Name = blockname;
btrec.DowngradeOpen();
foreach (ObjectId index in btrec)
{
Entity en = (Entity)index.GetObject(OpenMode.ForRead);
AttributeDefinition adef = en as AttributeDefinition;
if (adef != null)
{
ed.WriteMessage("\n" + adef.Tag);
}
}//<--- debug only
BlockTableRecord btr = (BlockTableRecord)curdb.CurrentSpaceId.GetObject(OpenMode.ForWrite);
using (btr)
{
using (BlockReference bref = new BlockReference(ipt, blkid))
{
Matrix3d mat = Matrix3d.Identity;
bref.TransformBy(mat);
bref.ScaleFactors = new Scale3d(1, 1, 1);
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
// To Explode the Block. * START.
using (DBObjectCollection dbObjctColcton = new DBObjectCollection())
{
bref.Explode(dbObjctColcton);
foreach (DBObject dbObj in dbObjctColcton)
{
Entity acEnt = dbObj as Entity;
btr.AppendEntity(acEnt);
tr.AddNewlyCreatedDBObject(dbObj, true);
acEnt = tr.GetObject(dbObj.ObjectId, OpenMode.ForWrite) as Entity;
}
}
// To Explode the Block. * END.
using (BlockTableRecord btAttRec = (BlockTableRecord)bref.BlockTableRecord.GetObject(OpenMode.ForRead))
{
Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;
foreach (ObjectId subid in btAttRec)
{
Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = ent as AttributeDefinition;
if (attDef != null)
{
// ed.WriteMessage("\nValue: " + attDef.TextString);
AttributeReference attRef = new AttributeReference();
attRef.SetPropertiesFrom(attDef);
attRef.Visible = attDef.Visible;
attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
attRef.HorizontalMode = attDef.HorizontalMode;
attRef.VerticalMode = attDef.VerticalMode;
attRef.Rotation = attDef.Rotation;
attRef.TextStyleId = attDef.TextStyleId;
attRef.Position = attDef.Position + ipt.GetAsVector();
attRef.Tag = attDef.Tag;
attRef.FieldLength = attDef.FieldLength;
attRef.TextString = attDef.TextString;
attRef.AdjustAlignment(curdb);//?
atcoll.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
bref.BlockUnit = UnitsValue.Inches;
bref.DowngradeOpen();
}
}
btrec.DowngradeOpen();
bt.DowngradeOpen();
ed.Regen();
tr.Commit();
}
}
}
}
catch
{ Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog("No Templete or DWG is Open!");
}
finally
{
//
}
}
我还发现了如何打开新的空模板,即:
public static void NewDrawing()
{
string strTemplatePath = "acad.dwt";
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document acDoc = acDocMgr.Add(strTemplatePath);
acDocMgr.MdiActiveDocument = acDoc;
}
对于亲AutoCAD插件开发人员来说,也许很容易退出,但是AutoCAD-API对我来说是新的,我非常感谢您的帮助。