.NET

Reply
Member
rickso2604
Posts: 3
Registered: ‎12-18-2008
Message 1 of 7 (970 Views)

C# - set UCS by Object

970 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!

 

-Rick-

*Expert Elite*
norman.yuan
Posts: 987
Registered: ‎04-27-2009
Message 2 of 7 (961 Views)

Re: C# - set UCS by Object

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

Yes.

Editor.CurrentUserCoordinateSystem { set; get }

Member
rickso2604
Posts: 3
Registered: ‎12-18-2008
Message 3 of 7 (952 Views)

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)

 

Thanks!

-Rick-

Member
rickso2604
Posts: 3
Registered: ‎12-18-2008
Message 4 of 7 (899 Views)

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
        acUCSTbl.UpgradeOpen();

        // Add the new UCS table record
        acUCSTbl.Add(acUCSTblRec);
        acTrans.AddNewlyCreatedDBObject(acUCSTblRec, true);
    }
    else
    {
        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
    acVportTblRec.SetUcs(acUCSTblRec.ObjectId);
    acDoc.Editor.UpdateTiledViewportsFromDatabase();

    // Save the new object to the database
    acTrans.Commit();

}//using

 

Thanks!

 

-Rick-

Active Member
veersh
Posts: 7
Registered: ‎11-10-2007
Message 5 of 7 (677 Views)

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
veersh
Posts: 7
Registered: ‎11-10-2007
Message 6 of 7 (676 Views)

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
Balaji_Ram
Posts: 693
Registered: ‎03-21-2011
Message 7 of 7 (638 Views)

Re: C# - set UCS by Object

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

Hi,

 

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.

 

[CommandMethod("SetUCS")] 
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.Display(true); 
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; 
acadDoc.Regen(AcRegenType.acActiveViewport); 

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

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.