How to change UCS for model space inside viewport ?

How to change UCS for model space inside viewport ?

nghiabt04
Advocate Advocate
574 Views
6 Replies
Message 1 of 7

How to change UCS for model space inside viewport ?

nghiabt04
Advocate
Advocate

Hi,

Currently, I want to change UCS of model space in viewport but don't know how to do.

I found the method 

.SetUcs()

 but it's not working.

Does anyone knows to do it, please advise me.

Thanks and Regards.

nghiabt04_0-1674983379774.png

 

0 Likes
Accepted solutions (1)
575 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

The Viewport type has a Ucs read/write property.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

nghiabt04
Advocate
Advocate
Hi @_gile,
I also try this but see nothing change.
0 Likes
Message 4 of 7

_gile
Consultant
Consultant

Hi,

Most often when dealing with viewport, we need to swich them off and on to update the changes.

Try this way:

vp.On = false;
vp.Ucs = ucs;
vp.On = true;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 7

ivanstarr3
Contributor
Contributor

I'm also having the same problem but in pure model space (tilemode=1).  Also, after including the code in my project, the plugin made autocad run very slow.  very strange.  I will include the code below.  Thanks in advance Mr. Chanteau, or whoever else helps!  Also, how do I create that code window that you have your code example in above?

 

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

namespace ArchitecturalWindows
{
public class UCSCommands
{
private static ObjectId previousUcsId = ObjectId.Null; // Store the previous UCS ObjectId
private static bool wasWorldUCS = true; // Track if the previous UCS was World UCS


public static void SetNewUCS()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Get the UCS table
UcsTable ucsTable = tr.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;

// Get the current viewport settings
ObjectId viewportId = db.CurrentViewportTableRecordId;
ViewportTableRecord vpRecord = tr.GetObject(viewportId, OpenMode.ForWrite) as ViewportTableRecord;
previousUcsId = vpRecord.UcsName;
// Store the current UCS
if (ucsTable.Has(vpRecord.UcsName))
{
previousUcsId = vpRecord.UcsName;
wasWorldUCS = false;
}
else
{
previousUcsId = ObjectId.Null;
wasWorldUCS = true;
}

// Define a new UCS
string ucsName = "MyNewUCS";
UcsTableRecord newUcs;

if (ucsTable.Has(ucsName))
{
newUcs = tr.GetObject(ucsTable[ucsName], OpenMode.ForRead) as UcsTableRecord;
}
else
{
ucsTable.UpgradeOpen();
newUcs = new UcsTableRecord
{
Name = ucsName,
Origin = new Point3d(10, 10, 0), // Example new origin
XAxis = new Vector3d(0, 0, 1),
YAxis = new Vector3d(0, 1, 0)
};
ucsTable.Add(newUcs);
tr.AddNewlyCreatedDBObject(newUcs, true);
}

// Set the new UCS to the viewport
vpRecord.SetUcs(newUcs.ObjectId);
vpRecord.UcsSavedWithViewport = true; // Mark UCS as saved with this viewport

tr.Commit();
ed.WriteMessage("\nNew UCS set as current.");
}
}


}

 

 

 

 

0 Likes
Message 6 of 7

_gile
Consultant
Consultant
Accepted solution

Hi,

The Database.CurrentViewportTableRecordId property is not reliable because it does not update correctly.

To set a new UCS in the current viewport, you can simply set the Editor.CurrentCoordinateSystem property.

public static void SetUCS(Point3d origin, Vector3d xAxis, Vector3d yAxis)
{
    var zAxis = xAxis.CrossProduct(yAxis);
    Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem = 
        Matrix3d.AlignCoordinateSystem(
            Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis,
            origin, xAxis.GetNormal(), yAxis.GetNormal(), zAxis.GetNormal());
}

 


@ivanstarr3 wrote:

Also, how do I create that code window that you have your code example in above?


1. Hit the "Expand toolbar" button:

_gile_0-1740577017437.png

2. Hit the "Insert/Edit code sample" button:

_gile_1-1740577149077.png

3. Choose the language and past the code:

_gile_2-1740577249111.png

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 7 of 7

ivanstarr3
Contributor
Contributor

Wow thanks Mr. Chanteau!  So fast a response!

0 Likes