Author Topic: How to align ucs with solid in C#  (Read 1256 times)

0 Members and 1 Guest are viewing this topic.

johnpolob

  • Newt
  • Posts: 29
How to align ucs with solid in C#
« on: December 08, 2018, 08:14:17 PM »
Hello everyone, I have to calculate in C # the width and the length of many solids in my drawing. But some of them are not in the same plane, for example a solid inclined relative to the z axis and therefore the dimension is wrong. I would have liked to change some ucs and calculate the width of this entity to be correct.
Any help would be welcome.

MickD

  • Gator
  • Posts: 3282
  • (x-in)->[process]->(y-out)
Re: How to align ucs with solid in C#
« Reply #1 on: December 08, 2018, 10:48:15 PM »
this thread should be of some help, cheers.

https://www.theswamp.org/index.php?topic=51518.0

<added>
If you need to process existing 3dsolids then you will need the user to pick 3 points to define at least 2 axis vectors (say the base point of a face then a point along an edge for the Z axis for extrusion length then the third point anywhere else on the face to produce the width axis), the third axis is a simple normal calc to produce it.
« Last Edit: December 08, 2018, 10:52:25 PM by MickD »
Debugging:
Being the detective in a crime movie where you're also the murderer.

“Someone's sitting in the shade today because someone planted a tree a long time ago.”
- Warren Buffet

gile

  • Water Moccasin
  • Posts: 2218
  • Marseille, France
Re: How to align ucs with solid in C#
« Reply #2 on: December 09, 2018, 02:43:39 AM »
Hello everyone, I have to calculate in C # the width and the length of many solids in my drawing. [...]

You should clarify this because "the width and the length" of an entity does not depend on a coordinate system (e.g. the length of a line remains the same whatever the coodinate system).
So, you should describe with more details how you measure these solids.
Speaking English as a French Frog

johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #3 on: December 09, 2018, 08:56:56 AM »
Hi Gile,

Thank you for this fast reply.

To measure my solids (entity), I use the code below and It's working well with the standard solids that are not oblique:

public double EntityWidth(Entity ent)
            {
            double Width;
            Extents3d ext = ent.GeometricExtents;
            Width = ext.MaxPoint.X - ext.MinPoint.X;

            return Width;
            }

My problem is that I don't want to calculate myself the hypotenuse to have my solid length. It's the reason why I was believe that I can only change the ucs then calculate the length of oblique solid basing on this one.

If you have another way the get out this problem, I will be very happy to know It...

Thank you in advance for anyone who can help me.
« Last Edit: December 09, 2018, 09:57:22 AM by johnpolob »

johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #4 on: December 09, 2018, 09:09:28 AM »
Hi MickD,

I will check this solution tomorrow and let you know if It's working...

Thank you...

gile

  • Water Moccasin
  • Posts: 2218
  • Marseille, France
Re: How to align ucs with solid in C#
« Reply #5 on: December 09, 2018, 11:12:39 AM »
Entity.GeometricExtents always creates the bounding box of the entity about WCS axes.

You can get the bounding box about the current UCS axes by transforming the entity (Solid3d or whatever else) from WCS to UCS, getting the GeometricExtents of the transformed solid and transform it back to WCS.

Here's a little example:
Code - C#: [Select]
  1.         static Extents3d GetExtentsAboutUCS(Entity entity)
  2.         {
  3.             var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
  4.             var ucs2wcs = ed.CurrentUserCoordinateSystem;
  5.             var wcs2ucs = ucs2wcs.Inverse();
  6.             Extents3d extents;
  7.             if (!entity.IsWriteEnabled)
  8.                 entity.UpgradeOpen();
  9.             entity.TransformBy(wcs2ucs);
  10.             try { extents = entity.GeometricExtents; }
  11.             finally { entity.TransformBy(ucs2wcs); }
  12.             return extents;
  13.         }
Speaking English as a French Frog

johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #6 on: December 09, 2018, 12:02:01 PM »
Hi Gile,

Thank you for this example.

I will try It tomorrow and let you know.


johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #7 on: December 10, 2018, 06:01:43 AM »
Hi Gile,

First of all, I would like to thank you for your help.

I try to use your function and it works well.

Now, I have another problem with the automatic detection of ucs of my entity.

Please, could you show me a way to do it?

ribarm

  • Water Moccasin
  • Posts: 2091
  • Marko Ribar, architect
Re: How to align ucs with solid in C#
« Reply #8 on: December 10, 2018, 06:34:44 AM »
3D SOLIDS don't have UCS property bounded to entity they represent... You could only try to align UCS to one of it's faces if it has them and then orient axis of UCS you want to some reference edge of face or its vertex... Sorry but I think you are stuck here IMHO...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #9 on: December 10, 2018, 07:25:14 AM »
Hi ribarm,

Thank you for your contribution.

I will try to define some conditions to determine the ucs of my entity (solid).

dgorsman

  • Water Moccasin
  • Posts: 2387
Re: How to align ucs with solid in C#
« Reply #10 on: December 10, 2018, 09:58:45 AM »
Hi ribarm,

Thank you for your contribution.

I will try to define some conditions to determine the ucs of my entity (solid).

One way to handle that would be controlled creation of the 3D solids, such that they include a proper set of XDATA.  There's a handful of data types which represent vectors that are automatically modified as the host entity is moved, rotated, and/or scaled.

But otherwise yeah - getting face data for arbitrary solids is not pretty at best.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #11 on: December 10, 2018, 02:28:27 PM »
Hi All,

I gave up the idea of ​​automatically detecting the ucs of my solid entities
But to calculate the length of the oblique entities, I try to calculate the hypotenuse knowing here that it is an isosceles triangle. So, according to Pythagoras' theorem, C² = A² + B²; but I know that A = B  then C = √2A².
The height on the Z axis that I consider is slightly larger than expected.
Here is the function that I use to have the height in Z
public double EntityHeight (Entity ent)
            {
            double Height;
            Extents3d ext = ent.GeometricExtents;
            Height = ext.MaxPoint.Z - ext.MinPoint.Z;
            return Height;
            }
My problem is that the result is always 14 mm higher than expected.
For example the hypotenuse should be 1178 and the height in Z 833 but my results are 1192 for the hypotenuse and 843 for Z.

Could somebody helps me to fix this issue or an another  way to calculate this.

kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1338
  • class keyThumper<T>:ILazy<T>
Re: How to align ucs with solid in C#
« Reply #12 on: December 10, 2018, 05:37:05 PM »
johnpolob,

It may help anyone who has time to look at this issue if you were to attach a basic simple drawing model showing the solid you have problems with.

Regards,
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

johnpolob

  • Newt
  • Posts: 29
Re: How to align ucs with solid in C#
« Reply #13 on: December 11, 2018, 02:37:30 AM »
Hi kdub,

Thank you very much for this good idea.

Please find in attached my DWG with a solid.

Let me know if you can fix this issue.

Thank you...

SEANT

  • Bull Frog
  • Posts: 324
Re: How to align ucs with solid in C#
« Reply #14 on: December 11, 2018, 04:03:17 AM »
That files gives the impression that your 3d modeling is more concerned with processed sheet stock rather than lengths of extrusions.  If that were the case then I'd recommend using the BoundaryRepresentation namespace functions to analyze the faces to find the two largest, see if they have Normals that are parallel, though not codirectional.  Once found, the solid can be transformed to have that vector aligned to the World Z. 

Subsequent analysis of the edges to find the longest (though, finding 2 edges that are perpendicular to themselves as well as the Z axis may be more accurate) and set that parallel to the X axis, perhaps.
Sean Tessier
AutoCAD 2016 Mechanical