Announcements
Due to scheduled maintenance, the Autodesk Community will be inaccessible from 10:00PM PDT on Oct 16th for approximately 1 hour. We appreciate your patience during this time.
.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Changing current layout get eSetFailed for Paper Space Layouts

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
Anonymous
2457 Views, 9 Replies

Changing current layout get eSetFailed for Paper Space Layouts

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.

9 REPLIES 9
Message 2 of 10
_gile
in reply to: Anonymous

Hi,

 

You should give a clearer and more detailed description of the context the exception occurs and, overall, provide the whole code because the snippet you show is not relevant.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 10
Anonymous
in reply to: _gile

Have updated so reads the drawing file prior to trying to change the layout.

Message 4 of 10
Anonymous
in reply to: Anonymous

Realise the issue now, the drawing I have read is not open so getting the exception.

Message 5 of 10
Anonymous
in reply to: Anonymous

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

Message 6 of 10
_gile
in reply to: Anonymous

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.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 10
Anonymous
in reply to: _gile

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);

Message 8 of 10
_gile
in reply to: Anonymous

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);
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 10
ActivistInvestor
in reply to: Anonymous

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);


 

Message 10 of 10
CADbloke
in reply to: Anonymous

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

- - - - - - -
working on all sorts of things including www.tvCAD.tv & www.CADreplace.com

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report