Author Topic: Howto: Set the CurrentUCS from UCSTableRecord ?  (Read 5930 times)

0 Members and 1 Guest are viewing this topic.

MarioR

  • Guest
Howto: Set the CurrentUCS from UCSTableRecord ?
« on: July 16, 2008, 09:00:24 AM »
Hello,

how can i set the CurrentUCS from a UCSTableRecord, or by named?


regards
Mario

fixo

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #1 on: July 16, 2008, 01:05:39 PM »
I'm not sure about that my code is elegant
but seems to be worked with named ucs
Just change the name of ucs

Code: [Select]
Imports System
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports acApp = Autodesk.AutoCAD.ApplicationServices.Application
Namespace CoordinateSysytemUtils
    Public Class UCSCommands
        Public Shared Sub SetNamedUCS()
            Dim doc As Document = acApp.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction
                    Dim utb As UcsTable = CType(tr.GetObject(db.UcsTableId, OpenMode.ForRead), UcsTable)
                    If utb.Has("NewUCS") Then
                        Dim itbr As UcsTableRecord = CType(tr.GetObject(utb.Item("NewUCS"), OpenMode.ForRead), UcsTableRecord)
                        Dim ucsMat As Matrix3d = Matrix3d.AlignCoordinateSystem( _
                        Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, _
                        itbr.Origin, itbr.XAxis, itbr.YAxis, itbr.XAxis.CrossProduct(itbr.YAxis))
                        ed.CurrentUserCoordinateSystem = ucsMat
                        ed.Regen()
                        tr.Commit()
                    End If
                End Using
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                MsgBox(ex.Message & ControlChars.CrLf & ex.StackTrace)
            End Try
        End Sub
    End Class
End Namespace

~'J'~

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #2 on: July 16, 2008, 01:34:47 PM »
Nice!

fixo

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #3 on: July 16, 2008, 02:26:43 PM »
C'mon, your code are much smarter than mine,
I like to learn something new from you
Thanks, Daniel  :-)
Regards,

~'J'~

MarioR

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #4 on: July 17, 2008, 07:04:23 AM »
Thanks Fixo,

your function set the matrix from the UCS but the UCS is not the active UCS.
See "Named UCS" Requester. I am think that is not implement in ACAD.Net

Thanks Mario

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #5 on: July 17, 2008, 07:32:06 AM »
Thanks Fixo,

your function set the matrix from the UCS but the UCS is not the active UCS.
See "Named UCS" Requester. I am think that is not implement in ACAD.Net

Thanks Mario

Hi Mario,

Fixo’s function does exactly what you requested, it sets the current UCS from a UCSTableRecord that is retrieved from the database by a ‘name’ (NewUCS).  It’s the same solution I came up with.  I guess I’m not quite sure what you’re asking , do you have any code that does this via another API?

Dan

MarioR

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #6 on: July 17, 2008, 08:02:47 AM »
Hi Dan.

I have now discovered where the problem lies.
If you are the code sets the ucs, than it the ucs in then requester "NamedUcs" active.
But the ad in the toolbar UCSII is not updated, there is always "unknown".
But this is just a blemish.

Thanks to all

Mario

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #7 on: July 17, 2008, 08:13:49 AM »
Ok I see what you are talking about. By using this method, the next time you type ucs at the prompt it says:

Quote
Current ucs name:  *NO NAME*

hmmmm

fixo

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #8 on: July 17, 2008, 08:31:55 AM »
I can't fix it too :(

~'J'~

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #9 on: July 17, 2008, 10:33:28 AM »
have a look at this and see if it will work for you

Code: [Select]
namespace ExecMethod
{
  public class Commands
  {
    [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
    EntryPoint = "?acedVportTableRecords2Vports@@YA?AW4ErrorStatus@Acad@@XZ")]
    internal static extern ErrorStatus acedVportTableRecords2Vports();

    [CommandMethod("doit")]
    static public void doit()
    {

      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      Database db = HostApplicationServices.WorkingDatabase;

      try
      {
        AcDb.TransactionManager manager = db.TransactionManager;
        using (Transaction transaction = manager.StartTransaction())
        {
          UcsTable ut = transaction.GetObject(db.UcsTableId, OpenMode.ForRead) as UcsTable;
          ViewportTable vt = transaction.GetObject(db.ViewportTableId, OpenMode.ForRead) as ViewportTable;
          ViewportTableRecord vtr = transaction.GetObject(vt["*Active"], OpenMode.ForWrite) as ViewportTableRecord;

          if (ut.Has("Dan"))
          {
            UcsTableRecord utr = transaction.GetObject(ut["Dan"], OpenMode.ForRead) as UcsTableRecord;

            if (utr != null)
            {
              vtr.SetUcs(utr.ObjectId);
              acedVportTableRecords2Vports();
            }
          }
          transaction.Commit();
        }
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage("\n" + ex.Message);
        ed.WriteMessage("\n" + ex.StackTrace);
      }
    }
  }
}
« Last Edit: July 17, 2008, 10:40:40 AM by Daniel »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #10 on: July 17, 2008, 10:43:28 AM »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #11 on: July 17, 2008, 10:45:14 AM »
Oh yeah, Welcome to the swamp Mario   :laugh:

fixo

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #12 on: July 17, 2008, 11:01:02 AM »
I Got the answer from this thread
http://discussion.autodesk.com/thread.jspa?messageID=406596

Daniel, thanks
Worked like a charm :)

Here is the same converted to VB.NET:
Code: [Select]
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports AcadRT = Autodesk.AutoCAD.Runtime
Imports AcadED = Autodesk.AutoCAD.EditorInput
Imports AcDB = Autodesk.AutoCAD.DatabaseServices
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.Windows
Imports acApp = Autodesk.AutoCAD.ApplicationServices.Application
Namespace ExecMethod

    Public Class UcsUtils

        <Runtime.InteropServices.DllImport("acad.exe", CallingConvention:=Runtime.InteropServices.CallingConvention.Cdecl, EntryPoint:="?acedVportTableRecords2Vports@@YA?AW4ErrorStatus@Acad@@XZ")> _
  Friend Shared Function acedVportTableRecords2Vports() As ErrorStatus
        End Function

        <CommandMethod("UCD")> _
        Public Shared Sub doit()
            Dim ed As Editor = acApp.DocumentManager.MdiActiveDocument.Editor
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Try
                Dim manager As AcDB.TransactionManager = db.TransactionManager
                ' Using
                Using transaction As Transaction = manager.StartTransaction
                    Try
                        Dim ut As UcsTable = CType(transaction.GetObject(db.UcsTableId, OpenMode.ForRead), UcsTable)
                        Dim vt As ViewportTable = CType(transaction.GetObject(db.ViewportTableId, OpenMode.ForRead), ViewportTable)
                        Dim vtr As ViewportTableRecord = CType(transaction.GetObject(vt("*Active"), OpenMode.ForWrite), ViewportTableRecord)
                        If ut.Has("Dan") Then
                            Dim utr As UcsTableRecord = CType(transaction.GetObject(ut("Dan"), OpenMode.ForRead), UcsTableRecord)
                            If Not (utr Is Nothing) Then
                                ed.CurrentUserCoordinateSystem = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, utr.Origin, utr.XAxis, utr.YAxis, utr.XAxis.CrossProduct(utr.YAxis))
                                vtr.SetUcs(utr.ObjectId)
                                acedVportTableRecords2Vports()
                            End If
                        End If
                        transaction.Commit()
                    Finally
                        CType(transaction, IDisposable).Dispose()
                    End Try
                End Using
            Catch ex As System.Exception
                ed.WriteMessage("" & Microsoft.VisualBasic.Chr(10) & "" + ex.Message)
                ed.WriteMessage("" & Microsoft.VisualBasic.Chr(10) & "" + ex.StackTrace)
            End Try
        End Sub
    End Class
End Namespace

~'J'~

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8698
  • AKA Daniel
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #13 on: July 17, 2008, 11:11:45 AM »
Sorry I changed the code  :oops:
You no longer need to modify
Code: [Select]
ed.CurrentUserCoordinateSystem

MarioR

  • Guest
Re: Howto: Set the CurrentUCS from UCSTableRecord ?
« Reply #14 on: July 18, 2008, 12:36:38 AM »
Thank you, it works beautifully.

You are my heroes.

 :angel: