.NET

Reply
Contributor
Predator_mpm
Posts: 17
Registered: ‎11-30-2012
Message 1 of 10 (410 Views)
Accepted Solution

Copy or Clone a viewport

410 Views, 9 Replies
09-28-2013 02:35 AM

What is the correct way of copying an existing viewport, What I am actually doing is that to clone a layout by copying what is inside it, everything works smoothly except for the viewports.

I tried to use the clone method, but it didnt work, I also tried to use the copyfrom method but also to no avail.

In both cases the copied viewport is a dumb black rectangle

 

 

Dim exkeyvport As Autodesk.AutoCAD.DatabaseServices.Viewport = DirectCast(acTrans.GetObject(objId, OpenMode.ForRead), Autodesk.AutoCAD.DatabaseServices.Viewport)
                        Dim keyvport As New Autodesk.AutoCAD.DatabaseServices.Viewport

                        keyvport.CopyFrom(exkeyvport)
                        keyvport.UpdateDisplay()
                        NewblkTableRec.AppendEntity(keyvport)
                        acTrans.AddNewlyCreatedDBObject(keyvport, True)

 The only way that partially worked for me was to create a viewport and copy its attributes one by one, but this is exhaustive and not reliable, so could anyone tell us of the proper way to copy a viewport?

Hi,

 

The easiest way to do that is to create a new viewport and set its view parameters based on the source viewport.

 

BlockTableRecord btrDest = (BlockTableRecord)Tx.GetObject(LayoutDest.BlockTableRecordId, OpenMode.ForWrite);
Viewport vp = Tx.GetObject(per.ObjectId, OpenMode.ForRead) as Viewport;
Viewport vpNew = new Viewport();
vpNew.SetDatabaseDefaults();
Point2d viewCenter = vp.ViewCenter;
vpNew.ViewCenter = viewCenter;
Point3d viewTarget = vp.ViewTarget;
vpNew.ViewTarget = viewTarget;
Vector3d viewDir = vp.ViewDirection;
vpNew.ViewDirection = viewDir;
double viewHeight = vp.ViewHeight;
vpNew.ViewHeight = viewHeight;
double viewportHeight = vp.Height;
vpNew.Height = viewportHeight;
double viewportWidth = vp.Width;
vpNew.Width = viewportWidth;
Point3d centerPoint = vp.CenterPoint;
vpNew.CenterPoint = centerPoint;
double custScale = vp.CustomScale;
vpNew.CustomScale = custScale;
double elevation = vp.Elevation;
vpNew.Elevation = elevation;
double lensLength = vp.LensLength;
vpNew.LensLength = lensLength;
bool isVisible = vp.Visible;
vpNew.Visible = isVisible;
oid = btrDest.AppendEntity(vpNew);
Tx.AddNewlyCreatedDBObject(vpNew, true);

 

 

I was finally able to clone the viewport, without having the need to copy each parameter alone, the way to copy a viewport is by using the deepcloneobjects method

 

'objid refers to the viewport object id
If (objId.ObjectClass.DxfName.ToUpper = "VIEWPORT") Then
Dim id As ObjectId = newLayout.BlockTableRecordId
Dim idcol As New ObjectIdCollection
idcol.Add(objId)
Dim idmap As New IdMapping
acCurdb.DeepCloneObjects(idcol, acCurdb.CurrentSpaceId, idmap, False)
End if 

 The only bad thing in using this method is that I do not get the objectid of the newly created viewport, so that I can not modify it after cloning it (I wonder if there is a method to do so).

 

The viewport object also has a method that is deepclone(), I tried to use it but I didnt find enough documentation on how to use it, and I didn't reach a notable result.

 

Thanks for your answers guys

Glad to know that you have another solution in hand.

 

To get the ObjectId, look for the idmap. That should have that info.

 

Regards,

 

Contributor
Predator_mpm
Posts: 17
Registered: ‎11-30-2012
Message 2 of 10 (367 Views)

Re: Copy or Clone a viewport

10-02-2013 12:18 AM in reply to: Predator_mpm

Please some one answer!

New Member
Izz_acad
Posts: 1
Registered: ‎12-29-2009
Message 3 of 10 (359 Views)

Re: Copy or Clone a viewport

10-02-2013 02:42 AM in reply to: Predator_mpm

Add this :

keyvport.CenterPoint = New Point3d(keyvportCenterX, keyvportCenterY, 0)

            keyvport.Width = keyvportWidth         

    keyvport.Height = keyvportHeight

            keyvport.Layer = "0"

            keyvport.StandardScale = StandardScaleType.Scale1To1

                  keyvport.ViewDirection = Vector3d.ZAxis         

    keyvport.ViewTarget = New Point3d(0, 0, 0)           

  keyvport.ViewCenter = Point2d.Origin

            keyvport.Visible = True            

keyvport.On = True            

keyvport.UpdateDisplay()

Contributor
Predator_mpm
Posts: 17
Registered: ‎11-30-2012
Message 4 of 10 (354 Views)

Re: Copy or Clone a viewport

10-02-2013 03:07 AM in reply to: Izz_acad
You are changing a lot of the viewport original attributes, so this would not achieve my goals.
ADN Support Specialist
Balaji_Ram
Posts: 749
Registered: ‎03-21-2011
Message 5 of 10 (322 Views)

Re: Copy or Clone a viewport

10-04-2013 02:36 AM in reply to: Predator_mpm

Hi,

 

The easiest way to do that is to create a new viewport and set its view parameters based on the source viewport.

 

BlockTableRecord btrDest = (BlockTableRecord)Tx.GetObject(LayoutDest.BlockTableRecordId, OpenMode.ForWrite);
Viewport vp = Tx.GetObject(per.ObjectId, OpenMode.ForRead) as Viewport;
                    
Viewport vpNew = new Viewport();
vpNew.SetDatabaseDefaults();

Point2d viewCenter = vp.ViewCenter;
vpNew.ViewCenter = viewCenter;

Point3d viewTarget = vp.ViewTarget;
vpNew.ViewTarget = viewTarget;

Vector3d viewDir = vp.ViewDirection;
vpNew.ViewDirection = viewDir;

double viewHeight = vp.ViewHeight;
vpNew.ViewHeight = viewHeight;

double viewportHeight = vp.Height;
vpNew.Height = viewportHeight;

double viewportWidth = vp.Width;
vpNew.Width = viewportWidth; 

Point3d centerPoint = vp.CenterPoint;
vpNew.CenterPoint = centerPoint;

double custScale = vp.CustomScale;
vpNew.CustomScale = custScale;

double elevation = vp.Elevation;
vpNew.Elevation = elevation;

double lensLength = vp.LensLength;
vpNew.LensLength = lensLength;

bool isVisible = vp.Visible;
vpNew.Visible = isVisible;

oid = btrDest.AppendEntity(vpNew);
Tx.AddNewlyCreatedDBObject(vpNew, true);

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Contributor
Predator_mpm
Posts: 17
Registered: ‎11-30-2012
Message 6 of 10 (318 Views)

Re: Copy or Clone a viewport

10-04-2013 03:22 AM in reply to: Izz_acad

I was finally able to clone the viewport, without having the need to copy each parameter alone, the way to copy a viewport is by using the deepcloneobjects method

 

'objid refers to the viewport object id
If (objId.ObjectClass.DxfName.ToUpper = "VIEWPORT") Then
                    Dim id As ObjectId = newLayout.BlockTableRecordId
                    Dim idcol As New ObjectIdCollection
                    idcol.Add(objId)
                    Dim idmap As New IdMapping
                    acCurdb.DeepCloneObjects(idcol, acCurdb.CurrentSpaceId, idmap, False)
End if 

 The only bad thing in using this method is that I do not get the objectid of the newly created viewport, so that I can not modify it after cloning it (I wonder if there is a method to do so).

 

The viewport object also has a method that is deepclone(), I tried to use it but I didnt find enough documentation on how to use it, and I didn't reach a notable result.

 

Thanks for your answers guys

ADN Support Specialist
Balaji_Ram
Posts: 749
Registered: ‎03-21-2011
Message 7 of 10 (315 Views)

Re: Copy or Clone a viewport

10-04-2013 03:24 AM in reply to: Predator_mpm

Glad to know that you have another solution in hand.

 

To get the ObjectId, look for the idmap. That should have that info.

 

Regards,

 



Balaji
Developer Technical Services
Autodesk Developer Network

Contributor
Predator_mpm
Posts: 17
Registered: ‎11-30-2012
Message 8 of 10 (301 Views)

Re: Copy or Clone a viewport

10-04-2013 05:12 AM in reply to: Balaji_Ram

Dear Mr

 

I have checked your post here:

http://adndevblog.typepad.com/autocad/2012/07/restore-previously-saved-ucs-using-objectarx.html

 

and your note:  This solution does not change the UCS name that is displayed by AutoCAD using the UCSNAME system variable.

 


Is there a solution that does the same but shows the ucs name when using the ucsname variable?

 

Thanks for help

 

ADN Support Specialist
Balaji_Ram
Posts: 749
Registered: ‎03-21-2011
Message 9 of 10 (298 Views)

Re: Copy or Clone a viewport

10-04-2013 05:40 AM in reply to: Predator_mpm

My kind request would be to create a separate forum post for this topic.

 

It makes it easier for someone searching for answers later and also it is likely that you will get more than one way to solve it from the experts in this forum.

 

Regards,



Balaji
Developer Technical Services
Autodesk Developer Network

Contributor
Predator_mpm
Posts: 17
Registered: ‎11-30-2012
Message 10 of 10 (291 Views)

Re: Copy or Clone a viewport

10-04-2013 06:27 AM in reply to: Balaji_Ram
Sure
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.