3 Posts
1 Kudo
Registered: ‎12-18-2008
Post 1 of 7

C# - set UCS by Object

1108 Views, 6 Replies
07-19-2011 05:54 AM

Hi All,


Is there a way to set the UCS by selecting the object in c#? Thanks so much in advance!



*Expert Elite*
1,088 Posts
217 Kudos
Registered: ‎04-27-2009
Post 2 of 7

Re: C# - set UCS by Object

07-19-2011 07:30 AM in reply to: rickso2604


Editor.CurrentUserCoordinateSystem { set; get }

3 Posts
1 Kudo
Registered: ‎12-18-2008
Post 3 of 7

Re: C# - set UCS by Object

07-19-2011 08:18 AM in reply to: rickso2604

Thanks Norman!

This is what I have done;

Bla Bla - get entity;

editor.CurrentUserCoordinateSystem = Entity.ECS;


What happens is the UCS is set to the Entity's ECS which is exactly the same as the WCS..... Bottom line for me, it didn't change anything... (the object was drawn in 3D plan)




3 Posts
1 Kudo
Registered: ‎12-18-2008
Post 4 of 7

Re: C# - set UCS by Object

08-29-2011 07:47 AM in reply to: rickso2604

Hi All,


I have figured it out how to set the UCS to the Object's ECS... Here what I have done;


// Get the Entity - The object is a Circle drawn on a 3D plan
ObjectId LastDBEntID = SelectLastEnt(acDoc);
Entity ent = acGetEntity(LastDBEntID);

Point3d cenPt = ((Circle)ent).Center;

Vector3d Xaxis = ent.Ecs.CoordinateSystem3d.Xaxis;
Vector3d Yaxis = ent.Ecs.CoordinateSystem3d.Yaxis;

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    // Open the UCS table for read
    UcsTable acUCSTbl;
    acUCSTbl = acTrans.GetObject(acCurDb.UcsTableId, OpenMode.ForRead) as UcsTable;

    UcsTableRecord acUCSTblRec;

    // Check to see if the "New_UCS" UCS table record exists
    if (acUCSTbl.Has("New_UCS") == false)
        acUCSTblRec = new UcsTableRecord();
        acUCSTblRec.Name = "New_UCS";

        // Open the UCSTable for write

        // Add the new UCS table record
        acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true);
        acUCSTblRec = acTrans.GetObject(acUCSTbl["New_UCS"], OpenMode.ForWrite) as UcsTableRecord;

    acUCSTblRec.Origin = cenPt;
    acUCSTblRec.XAxis = Xaxis;
    acUCSTblRec.YAxis = Yaxis;

    // Open the active viewport
    ViewportTableRecord acVportTblRec;
    acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;

    // Set the UCS current

    // Save the new object to the database






Active Member
7 Posts
0 Kudos
Registered: ‎11-10-2007
Post 5 of 7

Re: C# - set UCS by Object

11-06-2012 10:17 PM in reply to: rickso2604

Hi all,


acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord;


This statement is not working when I planned to set ucs to Layout Viewport.


any help Please?


Active Member
7 Posts
0 Kudos
Registered: ‎11-10-2007
Post 6 of 7

Re: C# - set UCS by Object

11-06-2012 10:19 PM in reply to: veersh

There is error like this


Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.Viewport' to type 'Autodesk.AutoCAD.DatabaseServices.ViewportTableRecord'.


ADN Support Specialist
762 Posts
100 Kudos
Registered: ‎03-21-2011
Post 7 of 7

Re: C# - set UCS by Object

11-09-2012 09:07 AM in reply to: veersh



Search for "Viewports" in the Developer's guide and you will find this explanation explaining the ViewportTableRecord and the Viewport. In paperspace, you will only find Viewports and not ViewportTableRecord.



A viewport is a particular view of the drawing file, either in model space (TILEMODE=1) or paper space (TILEMODE=0). In model and paper space, viewports have different properties and are represented by objects of different types.

The current model space viewport configuration is contained in the AcDbViewportTable symbol table. The viewports in this configuration are represented by AcDbViewportTableRecord objects. Saved viewport configurations are contained in the AcDbViewTable symbol table and their views are represented by AcDbViewTableRecord objects. The model space viewports are always rectangular.

In a paper space layout, viewports are represented by AcDbViewport objects. These viewports can be complex clipped, that is they can be set to any arbitrary shape. In paper space, unlike model space, viewports do not need to be tiled. AcDbLayout objects, accessed from the ACAD_LAYOUT named object dictionary, describe the viewport configuration for that layout.



There is a known issue in setting the UCS for a Viewport using its SetUCS method. As a workaround, set it using the COM API as shown in the sample code. You can modify it based on ECS value.


public void SetViewportUcs() 
Document doc = Application.DocumentManager.MdiActiveDocument; 
Editor oEd = Application.DocumentManager.MdiActiveDocument.Editor; 

AcadPViewport acadViewport = null; 
AcadDocument acadDoc = doc.AcadDocument as AcadDocument; 

acadDoc.ActiveLayout = acadDoc.Layouts.Item("Layout1"); 

//Reference the ViewPort object on Layout (the ActiveLayout) 
if (acadDoc.PaperSpace.Item(1).ObjectName == "AcDbViewport") 
acadViewport = acadDoc.PaperSpace.Item(1) as AcadPViewport; 

//Make sure the Viewport is displayed and on 
acadViewport.ViewportOn = true; 

//Before making a pViewport active the mspace property needs 
//to be True for a floating viewport 

acadDoc.MSpace = true; 
acadDoc.ActivePViewport = acadViewport; 

Matrix3d newUcsMat = Matrix3d.AlignCoordinateSystem(new Point3d(0, 0, 0), new Vector3d(1, 0, 0), new Vector3d(0, 1, 0), new Vector3d(0, 0, 1), 
new Point3d(0, 0, 0), new Vector3d(0, 1, 0), new Vector3d(-1, 0, 0), new Vector3d(0, 0, 1)); 

oEd.CurrentUserCoordinateSystem = newUcsMat; 




Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!