.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Viewport is off, after copying it to a new drawing

6 REPLIES 6
Reply
Message 1 of 7
Ertqwa
671 Views, 6 Replies

Viewport is off, after copying it to a new drawing

Hello Forum,

 

I create a new drawing from the active drawing (AutoCAD 2016). Then I copy a viewport from the active drawing to the new drawing. In the active drawing, the viewport is on, but in the new drawing the viewport is off.

 

If I check the viewport in the new drawing (while debugging), the property "Viewport.On" is true.

If I add code to set the viewport in the new drawing to true (Viewport.On = true;), it adds a new viewport (????).

 

Code fragments:

 

 

// New drawing.
docNew = Application.DocumentManager.Add(strTemplatePath);

// Get ObjectId's from active drawing (including viewports).
objBlockTable = objTransaction.GetObject(dbActive.BlockTableId, OpenMode.ForRead) as BlockTable;
objBlockTableRecord = objTransaction.GetObject(objLayoutItem.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
oidcLayoutsArray[intIndex] = new ObjectIdCollection();
foreach (ObjectId oidItem in objBlockTableRecord)
{
  oidcLayoutsArray[intIndex].Add(oidItem);
}

// Copy entities (including viewports).
objBlockTableRecord = objTransaction.GetObject(objBlockTable[BlockTableRecord.PaperSpace], OpenMode.ForRead) as BlockTableRecord;
objIdMapping = new IdMapping();
dbActive.WblockCloneObjects(oidcLayoutsArray[intIndex], objBlockTableRecord.ObjectId, objIdMapping, DuplicateRecordCloning.Ignore, false);

// Turn on viewport in new drawing.
foreach(ObjectId oidEntity in objBlockTableRecord)
{
  objViewPort = objTransaction.GetObject(oidEntity, OpenMode.ForWrite) as Viewport;
  if(objViewPort!=null)
  {
    objViewPort.On = true;
  }
  objViewPort = null;
}

// Save the new dwg.
docNew.Database.SaveAs(System.IO.Path.Combine(strDirectory, objLayoutItem.LayoutName + ".dwg"), true, DwgVersion.Current, docNew.Database.SecurityParameters);
docNew.CloseAndDiscard();

Can anyone explain how to copy a viewport from the active drawing to a new drawing, and turn it on?

 

Thank you.

6 REPLIES 6
Message 2 of 7
brianchapmandesign
in reply to: Ertqwa

Not .net but right-clicking over the layout tab and choosing "import from template" will import all layouts you want into a new drawing.


"Very funny, Scotty. Now beam down my clothes.
Message 3 of 7
ActivistInvestor
in reply to: Ertqwa

I can't make out from the fragments you posted a few things, like for example where did the objTransaction come from? What database?

 

And, is the new document that you copy the layouts to active when you call WblockCloneObjects(), and if so, is the destination layout active?

 


@Ertqwa wrote:

Hello Forum,

 

I create a new drawing from the active drawing (AutoCAD 2016). Then I copy a viewport from the active drawing to the new drawing. In the active drawing, the viewport is on, but in the new drawing the viewport is off.

 

If I check the viewport in the new drawing (while debugging), the property "Viewport.On" is true.

If I add code to set the viewport in the new drawing to true (Viewport.On = true;), it adds a new viewport (????).

 

 

Can anyone explain how to copy a viewport from the active drawing to a new drawing, and turn it on?

 

Thank you.


 

Message 4 of 7

believe you might be using undocumented functions or something but have you tried creating the copies in the current drawing then saving them out?


"Very funny, Scotty. Now beam down my clothes.
Message 5 of 7
Ertqwa
in reply to: ActivistInvestor

Hi,

 

Thank you for the response. The complete codes is as follows (some functions are user-functions, but nothing special). This should make clear your questions.

 

        [CommandMethod("SaveLayoutsAsDwgs", CommandFlags.Session)]
        public void SaveLayoutsAsDwgs()
        {
            AAA.Document docActive = null;
            string strActiveDwgPath = null;
            string strDirectory = null;
            AAA.DocumentLock dlActive = null;
            Transaction objTransaction = null;
            Database dbActive = null;
            string strTemplatePath = "Pisa-Template_M.dwt";
            List<Layout> objLayoutList = new List<Layout>();
            ObjectIdCollection oidcModeSpace = null;
            ObjectIdCollection[] oidcLayoutsArray = null;
            BlockTable objBlockTable = null;
            BlockTableRecord objBlockTableRecord = null;
            int intIndex = 0;
            DocumentLock objDocumentLock = null;
            IdMapping objIdMapping = null;
            AAA.Document docNew = null;
            AAA.Document docOriginal = null;
            Viewport objViewPort = null;

            try
            {
                // Remember what is the original active document.
                docOriginal = AAA.Application.DocumentManager.MdiActiveDocument;
                // Get active document.
                docActive = AAA.Application.DocumentManager.MdiActiveDocument;
                // Get path of active drawing.
                //  Read the Filename property of the Database to get the full path to the drawing.
                //  If the drawing is a new, unsaved document, the Filename property returns the .DWT file used to create the new drawing.
                strActiveDwgPath = docActive.Database.Filename;
                if(string.IsNullOrEmpty(strActiveDwgPath))
                {
                    CTSUtility.WriteMessageONL(" The current drawing must be saved first.");
                    CTSUtility.WriteMessageONL(" The new drawing(s) will be saved in the same folder.");
                    return;
                }
                else if (!strActiveDwgPath.EndsWith(".dwg", StringComparison.OrdinalIgnoreCase))
                {
                    CTSUtility.WriteMessageONL(" The current drawing must be saved first.");
                    CTSUtility.WriteMessageONL(" The new drawing(s) will be saved in the same folder.");
                    return;
                }
                // Get directory.
                strDirectory = System.IO.Path.GetDirectoryName(strActiveDwgPath);
                if (string.IsNullOrEmpty(strDirectory))
                {
                    CTSUtility.WriteMessageONL(" Could not extract the directory of the current drawing (" + strActiveDwgPath + ").");
                    return;
                }
                // Loop each layout.
                // Lock document.
                using (dlActive = docActive.LockDocument())
                {
                    // Get database of active dwg.
                    dbActive = AAA.Application.DocumentManager.MdiActiveDocument.Database;
                    // Start a transaction.
                    using (objTransaction = dbActive.TransactionManager.StartTransaction())
                    {
                        // Copy everything from modespace.
                        objBlockTable = objTransaction.GetObject(dbActive.BlockTableId, OpenMode.ForRead) as BlockTable;
                        objBlockTableRecord = objTransaction.GetObject(objBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                        oidcModeSpace = new ObjectIdCollection();
                        foreach (ObjectId oidItem in objBlockTableRecord)
                        {
                            oidcModeSpace.Add(oidItem);
                        }
                        objBlockTableRecord = null;
                        objBlockTable = null;
                        // Copy everything from each layout.
                        objLayoutList = CTSUtility.GetPaperspaceLayouts(objTransaction, dbActive);
                        oidcLayoutsArray = new ObjectIdCollection[objLayoutList.Count];
                        intIndex = 0;
                        foreach (Layout objLayoutItem in objLayoutList)
                        {
                            objBlockTable = objTransaction.GetObject(dbActive.BlockTableId, OpenMode.ForRead) as BlockTable;
                            objBlockTableRecord = objTransaction.GetObject(objLayoutItem.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
                            oidcLayoutsArray[intIndex] = new ObjectIdCollection();
                            foreach (ObjectId oidItem in objBlockTableRecord)
                            {
                                oidcLayoutsArray[intIndex].Add(oidItem);
                            }
                            objBlockTableRecord = null;
                            objBlockTable = null;
                            // Next.
                            intIndex++;
                        }
                    }
                }
                // Loop layouts.
                intIndex = 0;
                foreach (Layout objLayoutItem in objLayoutList)
                {
                    // Create new drawing with default tempate "Pisa-Template_M.dwt".
                    //  If the template is not found, the default settings are used.
                    using (docNew = AAA.Application.DocumentManager.Add(strTemplatePath))
                    {
                        AAA.Application.DocumentManager.MdiActiveDocument = docNew;
                        // Lock the new document.
                        using (objDocumentLock = docNew.LockDocument())
                        {
                            // Start a transaction in the new database
                            using (objTransaction = docNew.TransactionManager.StartTransaction())
                            {
                                // Open the Block table for read.
                                objBlockTable = objTransaction.GetObject(docNew.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                                // Copy modelspace entities.
                                objBlockTableRecord = objTransaction.GetObject(objBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
                                objIdMapping = new IdMapping();
                                dbActive.WblockCloneObjects(oidcModeSpace, objBlockTableRecord.ObjectId, objIdMapping, DuplicateRecordCloning.Ignore, false);
                                //
                                //
                                // Copy paperspace entities.
                                objBlockTableRecord = objTransaction.GetObject(objBlockTable[BlockTableRecord.PaperSpace], OpenMode.ForRead) as BlockTableRecord;
                                objIdMapping = new IdMapping();
                                dbActive.WblockCloneObjects(oidcLayoutsArray[intIndex], objBlockTableRecord.ObjectId, objIdMapping, DuplicateRecordCloning.Ignore, false);
                                // For some unknown reasons, viewports are turned off when copied.
                                //  Turn them on.
                                foreach(ObjectId oidEntity in objBlockTableRecord)
                                {
                                    objViewPort = objTransaction.GetObject(oidEntity, OpenMode.ForWrite) as Viewport;
                                    if(objViewPort!=null)
                                    {
                                        objViewPort.On = true;
                                    }
                                    objViewPort = null;
                                }
                                // Save the copied objects to the database
                                objTransaction.Commit();
                            }
                            // Zoom extents.
                            AAA.Application.DocumentManager.MdiActiveDocument = docNew;
                            AAA.Application.DocumentManager.MdiActiveDocument.Database.TileMode = true; // Modelspace.
                            (AAA.Application.AcadApplication as dynamic).ZoomExtents();
                            AAA.Application.DocumentManager.MdiActiveDocument.Database.TileMode = false; // Paperspace.
                            (AAA.Application.AcadApplication as dynamic).ZoomExtents();
                            // For some unknown reasons, viewports are turned off when copied.
                            //  Turn them on.
                            using (objTransaction = docNew.TransactionManager.StartTransaction())
                            {
                                objBlockTableRecord = objTransaction.GetObject(objBlockTable[BlockTableRecord.PaperSpace], OpenMode.ForRead) as BlockTableRecord;
                                foreach (ObjectId oidEntity in objBlockTableRecord)
                                {
                                    objViewPort = objTransaction.GetObject(oidEntity, OpenMode.ForWrite) as Viewport;
                                    if (objViewPort != null)
                                    {
                                        objViewPort.On = true;
                                    }
                                    objViewPort = null;
                                }
                                // Save to the database
                                objTransaction.Commit();
                            }
                        }
                        AAA.Application.DocumentManager.MdiActiveDocument = docOriginal;
                        // Save dwg.
                        docNew.Database.SaveAs(System.IO.Path.Combine(strDirectory, objLayoutItem.LayoutName + ".dwg"), true, DwgVersion.Current, docNew.Database.SecurityParameters);
                        docNew.CloseAndDiscard();
                    }
                    docNew = null;
                    // Next.
                    intIndex++;
                }
            }
            catch (System.Exception Ex)
            {
                CTSUtility.WriteMessageONL("Error : " + Ex.Message);
            }
        }
Message 6 of 7
Ertqwa
in reply to: brianchapmandesign

Hi,

 

I'm not sure what you mean with "creating the copies in the current drawing". They already exist in the active drawing from which I run the command.

Message 7 of 7
Ertqwa
in reply to: Ertqwa

Hello,

 

I found some answers, for any one who encounters the same problems:

 

I also copied the paperspace associated with the paperspace layout (see my next post on this forum).

 

Thank you.

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report