entity coordinates

entity coordinates

gwhitcherQ98HZ
Enthusiast Enthusiast
383 Views
1 Reply
Message 1 of 2

entity coordinates

gwhitcherQ98HZ
Enthusiast
Enthusiast

Is there a way of getting the coordinates of every entity. I know how to get the vertices coordinates of a polylines but I want to iterate through all entities in model space and move them to a 0 z coordinate if it is not at 0 already. 

0 Likes
384 Views
1 Reply
Reply (1)
Message 2 of 2

ActivistInvestor
Mentor
Mentor

All entities have at least one coordinate, but each class elaborates them  differently, so approaching the problem using coordinates is difficult as it requires you to deal with each type of entity using a different code path.

 

Here is a much simpler way to do it:

 

 

using Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;

namespace Autodesk.AutoCAD.DatabaseServices.MyEntityExtensions;

public static class EntityExtensions
{
   public static bool TranslateToXYPlane(this Entity entity)
   {
      if(entity == null)
         throw new ArgumentNullException(nameof(entity));
      double z = entity.GetBoundingBox().MinPoint.Z;
      if(Math.Abs(z) > 1.0e-5)
      {
         if(!entity.IsWriteEnabled)
            entity.UpgradeOpen();
         entity.TransformBy(Matrix3d.Displacement(new Vector3d(0, 0, z).Negate()));
         return true;
      }
      return false;
   }

   /// <comments>
   /// We can't universally use GeometryExtentsBestFit() on any
   /// block reference without risking an exception, which happens
   /// with non uniformly-scaled block references. This code tries 
   /// to work around it, but falls-back with non uniformly-scaled
   /// block references, resulting in a possibly-erroneous result.
   /// </comments>

   public static Extents3d GetBoundingBox(this Entity entity)
   {
      if(entity is BlockReference br)
      {
         try
         {
            return br.GeometryExtentsBestFit(Matrix3d.Identity);
         }
         catch(AcRx.Exception ex)
         {
            if(ex.ErrorStatus != AcRx.ErrorStatus.InvalidInput)  // NUS blockref
               throw ex;
         }
      }
      return entity.GeometricExtents;
   }
   
}

 

 

Open each each entity, and invoke the TranslateToXYPlane() extension method on it.