C# - set UCS by Object

C# - set UCS by Object

Anonymous
Not applicable
5,287 Views
6 Replies
Message 1 of 7

C# - set UCS by Object

Anonymous
Not applicable

Hi All,

 

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

 

-Rick-

0 Likes
5,288 Views
6 Replies
Replies (6)
Message 2 of 7

Norman_Yuan
Mentor
Mentor

Yes.

Editor.CurrentUserCoordinateSystem { set; get }

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 7

Anonymous
Not applicable

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-

0 Likes
Message 4 of 7

Anonymous
Not applicable

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-

Message 5 of 7

Anonymous
Not applicable

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?

 

0 Likes
Message 6 of 7

Anonymous
Not applicable

There is error like this

 

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

 

0 Likes
Message 7 of 7

Balaji_Ram
Alumni
Alumni

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