The following Code throws exception with eSetFailed when drawing saved in Model Space except when LayoutName is Model.
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
db.ReadDwgFile(fileName, System.IO.FileShare.Read, false, "");
db.CloseInput(true);
LayoutManager layoutManager = LayoutManager.Current;
DBDictionary layoutDict;
using (layoutDict = acTrans.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary)
{
foreach (DictionaryEntry de in layoutDict)
{
using (Layout layout = acTrans.GetObject((ObjectId)(de.Value), OpenMode.ForRead) as Layout)
{
try
{
layoutManager.CurrentLayout = layout.LayoutName;
}
catch (System.Exception)
{
}
}
}
}
acTrans.Commit();
}
I need to change to paper space when a drawing has been saved in Model Space, how can I achieve this given changing current layout won't work for me.
Solved! Go to Solution.
Solved by _gile. Go to Solution.
I have this OpenDrawing which reads the drawing file into a database object.
private Database OpenDrawing(string fileName, string boardName)
{
Database db = new Database(false, true);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
db.ReadDwgFile(fileName, System.IO.FileShare.Read, false, "");
db.CloseInput(true);
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in acBlkTblRec)
{
DBObject obj = tr.GetObject(id, OpenMode.ForRead);
BlockReference blkRef = obj as BlockReference;
if (blkRef != null)
{
if (blkRef.Name.Equals(boardName))
{
AttributeCollection attCol = blkRef.AttributeCollection;
foreach (ObjectId objID in attCol)
{
DBObject dbObj = tr.GetObject(objID, OpenMode.ForWrite) as DBObject;
AttributeReference acAttRef = dbObj as AttributeReference;
acAttRef.TextString = "";
}
}
}
}
tr.Commit();
}
return db;
}
My problem is after saving this drawing need to export to PDF using AcadDocument.SendCommand which hangs if the original drawing file was saved in Model Space. Now because the drawing is not the active document using LayoutManager gives me the errors described previously.
So either have to switch from Model Space to Paper Space in the database object or change drawing layout to Paper Space in AcadDocument SendCommand.
Have looked online for examples of how to do this but haven't found a solution as all require for the drawing to be the active document
I do not fully understand what do you try to achieve, but it seems you're following a wrong route.
On one side, you use a 'side database' with ReadDwgFile which only let you acces to the Database (not the document) and on the other side you want to use a COM document (AcadDocument) to run SendCommand.
What I am trying to do is when the drawing file that has been read in the above method was originally saved in Model Space, after updating the attributes and save with a new name to preferably switch to paper space, if the original file was saved in model space before saving the document.
Currently use this to save the document after updating the attributes:
db.SaveAs(newPath + ".dwg", true, DwgVersion.AC1027, db.SecurityParameters);
Using AcadDocument SendCommand as the easiest way to export the document as a PDF File see below:
AcadDocument doc = acadApp.Documents.Open(filePath, true);
doc.Activate();
doc.SendCommand("(command "-EXPORT"" ""P"" ""C"" ""N"" ""filePath.Replace("\\", "/").Replace(".dwg", ".pdf") "") ");
doc.Close(false);
Try something like this.
private void OpenDrawing(string fileName, string newPath, string boardName) { // store the current database Database curDb = HostApplicationServices.WorkingDatabase; using (Database db = new Database(false, true)) { db.ReadDwgFile(fileName, System.IO.FileShare.Read, false, ""); using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord acBlkTblRec = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; foreach (ObjectId id in acBlkTblRec) { DBObject obj = tr.GetObject(id, OpenMode.ForRead); BlockReference blkRef = obj as BlockReference; if (blkRef != null) { if (blkRef.Name.Equals(boardName)) { AttributeCollection attCol = blkRef.AttributeCollection; foreach (ObjectId objID in attCol) { DBObject dbObj = tr.GetObject(objID, OpenMode.ForWrite) as DBObject; AttributeReference acAttRef = dbObj as AttributeReference; acAttRef.TextString = ""; } } } } // set db as working database HostApplicationServices.WorkingDatabase = db; DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead); // set the first layout as current layout foreach (DBDictionaryEntry entry in layoutDict) { if (entry.Key != "Model") { LayoutManager.Current.CurrentLayout = entry.Key; break; } } tr.Commit(); } // reset the working database HostApplicationServices.WorkingDatabase = curDb; db.SaveAs(newPath + ".dwg", true, DwgVersion.AC1027, db.SecurityParameters); } }
If you are going to open the document in the editor after SaveAs(), there isn't really any point to changing the active layout via the Database. You can open the document, activate whatever layout you want, export to PDF and then save the document using the AcadDocument.Save() ActiveX method.
The Database.ReadDwgFile() method is mainly used to access DWG files without having to open them in the editor.
@Anonymouswrote:What I am trying to do is when the drawing file that has been read in the above method was originally saved in Model Space, after updating the attributes and save with a new name to preferably switch to paper space, if the original file was saved in model space before saving the document.
Currently use this to save the document after updating the attributes:
db.SaveAs(newPath + ".dwg", true, DwgVersion.AC1027, db.SecurityParameters);
Using AcadDocument SendCommand as the easiest way to export the document as a PDF File see below:
AcadDocument doc = acadApp.Documents.Open(filePath, true);
doc.Activate();doc.SendCommand("(command "-EXPORT"" ""P"" ""C"" ""N"" ""filePath.Replace("\\", "/").Replace(".dwg", ".pdf") "") ");
doc.Close(false);
It failed for me inside StartOpenCloseTransaction() but worked ok when I changed it to StartTransaction(). It would switch to model space but not to any layout, failing with eSetFailed
Can't find what you're looking for? Ask the community or share your knowledge.