Author Topic: WCS to UCS  (Read 5902 times)

0 Members and 1 Guest are viewing this topic.

bathepn

  • Guest
WCS to UCS
« on: July 28, 2015, 02:37:40 AM »
Hello

I need your help. I need to transform coordonate from WCS to UCS. Endead I calculate the coordonate of a MText in function of a poliline but with the UCS the North that I have calculated weren't the good North.

On the web I found the opposite : transform from UCS to WCS but impossible to find WCS to SCU.

Thanks for your ansewr.
Bathepn

I am really sorry for my bad English, I hope you have understood me :)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: WCS to UCS
« Reply #1 on: July 28, 2015, 05:31:30 AM »
Try something like
Code - C#: [Select]
  1. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  2. //....................
  3.  
  4.  
  5.     var editor  = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  6.     var wcs2ucs  = editor.CurrentUserCoordinateSystem.Inverse();
  7.     Point3d ucsPoint  = worldPoint.TransformBy(wcs2ucs);
  8.  
  9.  


added:
Welcome to theSwamp
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

bathepn

  • Guest
Re: WCS to UCS
« Reply #2 on: July 28, 2015, 05:43:17 AM »
I ever try it.

I try :
- Inverse()
- Transpose()
- Inverse().Transpose()
- Transpose.Inverse()

The result was on the coordonate the point (X_WCS, Y_WCS) was draw on the UCS with the coordonate WCS or inverse (Y in X and X in Y) but still on WCS

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: WCS to UCS
« Reply #3 on: July 28, 2015, 06:27:03 AM »
Hi,

Keep in mind, AutoCAD .NET API always uses WCS coordinates for 3d points except with the Editor (mainly AutoCAD commands inputs and PromptResult outputs).
Try to give some precisions about the context you get these points.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: WCS to UCS
« Reply #4 on: July 28, 2015, 06:50:16 AM »

Try this,
Post a drawing with the essential geometry and the UCS where you want it.

Post minimal workable code to extract the WCS point you want translate to the UCS.

Give any hard values you think will help.


I'm off to bed shortly, so someone else will probably resolve this for you from the data supplied.

Regards,

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: WCS to UCS
« Reply #5 on: July 28, 2015, 12:24:13 PM »
bathepn double posted in a French web site (CADxp), I replied him there because it's easier for both to speak french.
I should post here the solution if we can solve it.
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: WCS to UCS
« Reply #6 on: July 28, 2015, 08:49:10 PM »
Thanks gile.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

bathepn

  • Guest
Re: WCS to UCS
« Reply #7 on: July 29, 2015, 04:20:47 AM »
Like Gille said I made a double post to be sure to have an answer :)

I had one so I post here the other post because his solution is working :)

http://cadxp.com/topic/41628-de-wcs-vers-scu/page__gopid__234667

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: WCS to UCS
« Reply #8 on: July 29, 2015, 06:50:43 AM »
Hi,

The goal was to insert a MText object at the center of a polyline bounding box parallely to the current coordinates system X axis.

These two examples helped bathepn to solve its problem.

This first one inserts the mtext at the center of the polyline bbox and rotate it to be parallel too the current UCS Xaxis.
Code - C#: [Select]
  1.         [CommandMethod("CMD1")]
  2.         public void Cmd1()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             // select a polyline
  9.             PromptEntityOptions peo = new PromptEntityOptions("\nSélectionnez une polyligne: ");
  10.             peo.SetRejectMessage("Objet non valide.");
  11.             peo.AddAllowedClass(typeof(Polyline), true);
  12.             PromptEntityResult per = ed.GetEntity(peo);
  13.             if (per.Status != PromptStatus.OK) return;
  14.  
  15.             using (Transaction tr = db.TransactionManager.StartTransaction())
  16.             {
  17.                 // open the polyline
  18.                 Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
  19.  
  20.                 // calculate the center of the polyline bounding box
  21.                 Extents3d extents = pline.GeometricExtents;
  22.                 Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  23.  
  24.                 // add the mtext
  25.                 using (MText mtext = new MText())
  26.                 {
  27.                     mtext.SetDatabaseDefaults();
  28.                     mtext.Location = center;
  29.                     mtext.Contents = "Ceci est un test";
  30.                     BlockTableRecord curSpace =
  31.                         (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  32.                     curSpace.AppendEntity(mtext);
  33.                     tr.AddNewlyCreatedDBObject(mtext, true);
  34.  
  35.                     // rotate the mtext
  36.                     Vector3d xdir = (Point3d)Application.GetSystemVariable("UCSXDIR") - Point3d.Origin;
  37.                     double ucsRotation = Vector3d.XAxis.GetAngleTo(xdir, Vector3d.ZAxis);
  38.                     mtext.TransformBy(Matrix3d.Rotation(ucsRotation, Vector3d.ZAxis, center));
  39.                 }
  40.                 tr.Commit();
  41.             }
  42.         }

This other one illustrates Kerry's suggestion. It uses the Editor.CurrentCoordinatesSystem matrix to transform the mtext form UCS to WCS.
Before applying the transformation matrix, the insertion point have to be converted form WCS to UCS with the inverse matrix.
Code - C#: [Select]
  1.         [CommandMethod("CMD2")]
  2.         public void Cmd2()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.  
  8.             // select a polyline
  9.             PromptEntityOptions peo = new PromptEntityOptions("\nSélectionnez une polyligne: ");
  10.             peo.SetRejectMessage("Objet non valide.");
  11.             peo.AddAllowedClass(typeof(Polyline), true);
  12.             PromptEntityResult per = ed.GetEntity(peo);
  13.             if (per.Status != PromptStatus.OK) return;
  14.  
  15.             Matrix3d UCS2WCS = ed.CurrentUserCoordinateSystem;
  16.             Matrix3d WCS2UCS = UCS2WCS.Inverse();
  17.  
  18.             using (Transaction tr = db.TransactionManager.StartTransaction())
  19.             {
  20.                 // open the polyline
  21.                 Polyline pline = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForRead);
  22.  
  23.                 // calculate the center of the polyline bounding box
  24.                 Extents3d extents = pline.GeometricExtents;
  25.                 Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  26.  
  27.                 // convert the point coordinates into UCS
  28.                 center = center.TransformBy(WCS2UCS);
  29.  
  30.                 // add the mtext
  31.                 using (MText mtext = new MText())
  32.                 {
  33.                     mtext.Location = center;
  34.                     mtext.Contents = "Ceci est un test";
  35.                     BlockTableRecord curSpace =
  36.                         (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  37.                     curSpace.AppendEntity(mtext);
  38.                     tr.AddNewlyCreatedDBObject(mtext, true);
  39.  
  40.                     // apply a transformation matrix form UCS to WCS the mtext object
  41.                     mtext.TransformBy(UCS2WCS);
  42.                 }
  43.                 tr.Commit();
  44.             }
  45.         }
Speaking English as a French Frog