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

C# - set UCS by Object

6 REPLIES 6
Reply
Message 1 of 7
rickso2604
3803 Views, 6 Replies

C# - set UCS by Object

Hi All,

 

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

 

-Rick-

6 REPLIES 6
Message 2 of 7
norman.yuan
in reply to: rickso2604

Yes.

Editor.CurrentUserCoordinateSystem { set; get }

Message 3 of 7
rickso2604
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-

Message 4 of 7
rickso2604
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-

Message 5 of 7
veersh
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?

 

Message 6 of 7
veersh
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'.

 

Message 7 of 7
Balaji_Ram
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

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost