- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a custom command to handle part creations internally (in this sample code it's "CREATEBLOCK2").
- Create a simple object (circle, rectangle).
- Call CreateBlock2.
- Select the object's insertion point (center, bottom-left, whatever) and then the object itself.
- Object is made into a block with attributes, copied to the current directory's "xxx - Parts" folder, object removed then XREF'd back in from the newly created file.
Part of step 4 is opening a drawing template to WBLOCK the object into. Here in lies my problem.
In the attached solution there are 2 drawing template files. "acad.dwt" from the installed AutoCAD 2021 Mechanical instance. "BLANK_3.dwg" is a deleted/purged copy of one of our templates (sans the title block, layers, etc.).
If I test with the empty "acad.dwt" I get zero problems.
If I test with "BLANK_3.dwg" it will regularly fail, but not guaranteed.
I think the issue is in the "UpdateMaster()" function's trans.Commit() (or really non-disposed objects?).
private bool UpdateMaster(ObjectId blockID, string outputFileName)
{
string blockName = txtBlockName.Text.Trim();
string blockDesc = txtBlockDescription.Text.Trim();
string partTemplate = txtPartTemplate.Text.Trim();
Document document = ACAD_APP.DocumentManager.MdiActiveDocument;
Database database = document.Database;
try
{
using (DocumentLock doclock = document.LockDocument())
{
using (Transaction trans = database.TransactionManager.StartTransaction())
{
//create variable used to determine is it is a block reference
Entity ent = trans.GetObject(blockID, OpenMode.ForRead, false) as Entity;
if (ent is BlockReference)
{
BlockReference blkRef = trans.GetObject(blockID, OpenMode.ForRead, false) as BlockReference;
BlockReference delBlkRef = trans.GetObject(blockID, OpenMode.ForWrite, false) as BlockReference;
Autodesk.AutoCAD.DatabaseServices.AttributeCollection attRefIds = blkRef.AttributeCollection;
//cycle through the attributes associated with the current block
foreach (ObjectId attrefid in attRefIds)
{
AttributeReference attref = (AttributeReference)trans.GetObject(attrefid, OpenMode.ForRead, false);
switch (attref?.Tag)
{
//if block has attribute Name then create the wblock
case "Name":
{
string strBlockName = blkRef.Name;
bool ok = false;
//Add block objectID
ObjectIdCollection oidc2 = new ObjectIdCollection();
oidc2.Add(blkRef.ObjectId);
using (var DestData = new Database(false, false))
{
DestData.ReadDwgFile(partTemplate, FileShare.ReadWrite, false, string.Empty);
using (Transaction transNewDb = DestData.TransactionManager.StartTransaction())
{
BlockTable bt = transNewDb.GetObject(DestData.BlockTableId, OpenMode.ForRead) as BlockTable;
IdMapping IDMap = new IdMapping();
DestData.WblockCloneObjects(oidc2, bt[BlockTableRecord.ModelSpace], IDMap, DuplicateRecordCloning.Replace, false);
if (!oidc2.IsDisposed)
{
oidc2.Dispose();
}
if (!IDMap.IsDisposed)
{
IDMap.Dispose();
}
CreatePartDrawingProps(transNewDb, DestData, attRefIds, blockName, blockDesc);
transNewDb.Commit();
DestData.SaveAs(outputFileName, DwgVersion.AC1027);
DeleteBlockRef(delBlkRef);
delBlkRef.Erase(true);
ok = true;
}
}
//Was wBlock successful?
if (ok)
{
BlockTable xrefBt = (BlockTable)trans.GetObject(database.BlockTableId, OpenMode.ForRead);
BlockTableRecord btrMs2 = (BlockTableRecord)trans.GetObject(xrefBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//create objectID for XRef
string fileName = Path.GetFileNameWithoutExtension(outputFileName);
ObjectId xrid = database.AttachXref(outputFileName, fileName);
//create block reference, reference the xref objectID
using (var bref = new BlockReference(Point3d.Origin, xrid))
{
bref.Rotation = blkRef.Rotation;
bref.ScaleFactors = blkRef.ScaleFactors;
//append xref to blocktable
btrMs2.AppendEntity(bref);
string blname = $"XRef - {Path.GetFileNameWithoutExtension(outputFileName)}";
//Get the layer table first...
LayerTable lt = (LayerTable)trans.GetObject(database.LayerTableId, OpenMode.ForRead);
ObjectId layerId = ObjectId.Null;
//Check if Layer exists...
if (lt.Has(blname))
{
layerId = lt[blname];
}
else
{
//If not, create the layer here.
lt.UpgradeOpen();
using (var ltr = new LayerTableRecord())
{
ltr.Name = blname; // Set the layer name
ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 7);
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
lt.DowngradeOpen();
}
bref.LayerId = layerId;
trans.AddNewlyCreatedDBObject(bref, true);
layerId = ObjectId.Null;
lt.Dispose();
}
xrid = ObjectId.Null;
btrMs2.Dispose();
}
}
break;
}
attref.Dispose();
}
delBlkRef.Dispose();
blkRef.Dispose();
}
ent.Dispose();
trans.Commit(); // ***** This will randomly fail!
}
}
}
catch (System.Exception ex)
{
ACAD_APP.ShowAlertDialog("Error occurred: " + ex.ToString() + "\n" + ex.StackTrace);
return false;
}
return true;
}
Solved! Go to Solution.