Author Topic: How to create a Dimension on plan XZ or YZ ?  (Read 7263 times)

0 Members and 1 Guest are viewing this topic.

bikelink

  • Guest
How to create a Dimension on plan XZ or YZ ?
« on: February 12, 2009, 12:38:37 PM »
I'm not able to build correctly an alingeddimension on plan xz.
It is always distant in y from the 2 original point .
I've tried to pass as argument  also the normal vector ..but isn't correct. the dimlinepoint is always distant in a dimension..
also if i set this to Xlinepoint1 or XlinePoint2..


AlignedDimension dim1 = new AlignedDimension();
                dim1.SetDatabaseDefaults();             
                dim1.XLine1Point = duepunti.PuntoInizio;
                dim1.XLine2Point = duepunti.PuntoFine;
                dim1.DimLinePoint = PtLineaQuota;
                dim1.Normal = vnormal;
                dim1.DimensionText = testoquota;
                dim1.TextRotation = textrotation;


Thank you ...

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8740
  • AKA Daniel
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #1 on: February 12, 2009, 04:51:38 PM »
how about this hack   :laugh:

Code: [Select]

namespace ExecMethod
{
  public static class Commands
  {
    [CommandMethod("doit")]
    public static void doit()
    {
      Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        Point3d p1 = new Point3d(0, 0 ,0);
        Point3d p2 = new Point3d(100, 100,0);

        AlignedDimension Dim1 = CreateAlignedDimension(p1,p2,10.0);
        AlignedDimension Dim2 = CreateAlignedDimension(p1, p2, -10.0);

        Dim1.AddToModelSpace();
        Dim2.AddToModelSpace();
      }
      catch (System.Exception ex)
      {
        ed.WriteMessage(ex.Message);
        ed.WriteMessage(ex.StackTrace);
      }
    }

    internal static AlignedDimension CreateAlignedDimension(Point3d p1, Point3d p2, double offset)
    {
      AlignedDimension dim = new AlignedDimension();
      dim.XLine1Point = p1;
      dim.XLine2Point = p2;

      using (Line line = new Line(p1, p2))
      {
        using (Curve c = line.GetOffsetCurves(offset)[0] as Curve)
        {
          dim.DimLinePoint = c.GetPointAtParameter(p1.DistanceTo(p2) / 2);
        }
      }
      return dim;
    }

    internal static ObjectId AddToModelSpace(this Entity ent)
    {
      ObjectId objId = ObjectId.Null;
      if (ent == null)
      {
        throw new ArgumentNullException();
      }
      Database Db = HostApplicationServices.WorkingDatabase;
      AcDb.TransactionManager Tm = Db.TransactionManager;
      using (Transaction tr = Tm.StartTransaction())
      {
        BlockTable tb = (BlockTable)tr.GetObject
          (Db.BlockTableId, OpenMode.ForRead, false);
        objId = ((BlockTableRecord)tr.GetObject
          (tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false)).AppendEntity(ent);
        tr.AddNewlyCreatedDBObject(ent, true);
        tr.Commit();
      }
      return objId;
    }
  }
}



kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2142
  • class keyThumper<T>:ILazy<T>
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #2 on: February 12, 2009, 06:34:32 PM »
Daniel,
I saw 'Dim' in your code and almost had an accident. :)




ps
Quote
En route to Shanghai
Is there a DisneyWorld where you're going ?
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8740
  • AKA Daniel
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #3 on: February 12, 2009, 07:47:47 PM »
I saw 'Dim' in your code and almost had an accident. :)

 :lol:

Is there a DisneyWorld where you're going ?

Unfortunately it’s not built yet  :|

bikelink

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #4 on: February 13, 2009, 01:35:29 AM »
HI Daniel, thank you for your answer , but your code doesn't build a dimension on plan XZ...or yZ. The two dimensions are in the plan XY...
And I need to build dimensions in real 3d space.


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8740
  • AKA Daniel
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #5 on: February 13, 2009, 07:45:15 AM »
Doh! Sorry  :oops:

bikelink

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #6 on: February 14, 2009, 02:01:40 AM »
no one in the world..can help me?  :cry:

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #7 on: February 14, 2009, 05:10:47 AM »
Hi,

I'am not already comfortable with .NET, so I just edit Daniel's code using a 'TransformBy' way (maybe there's a shorter one).

I added a parameter to CreateAlignedDimension method: the plane where to create the dimension.
In this method, I transform points WCS coordinates to the plane coordinates (the transformed points must have a 0.0 Z coordinate).
After the dimension is created (as a 2d entity), I transfrom it back to WCS.

In the 'doit' method the planes are defined using  its normal vector and use one of the dimension point coordinate to build the origin (to get the plane elevation).
I had also some informations on command line about transformed points (note they all have a 0.0 Z coordinate)

Code: [Select]
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace ExecMethod
{
    public static class Commands
    {
        [CommandMethod("doit")]
        public static void doit()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            try
            {
                Point3d p1 = new Point3d(0, 0, 0);
                Point3d p2 = new Point3d(0, 100, 100);
                // define the dimension plane: YZ plane, origin = 0,0,0
                Plane yzPlane = new Plane(p1, new Vector3d(1, 0, 0));

                Point3d p3 = new Point3d(10, 20, 30);
                Point3d p4 = new Point3d(60, 20, 50);
                // define the dimension plane: parallel to ZX plane, origin = 10,20,30, IOW elevation = 20
                Plane zxPlane = new Plane(p3, new Vector3d(0, -1, 0));

                AlignedDimension Dim1 = CreateAlignedDimension(p1, p2, 10.0, yzPlane);
                AlignedDimension Dim2 = CreateAlignedDimension(p3, p4, 10.0, zxPlane);

                Dim1.AddToModelSpace();
                Dim2.AddToModelSpace();

                ed.WriteMessage("p1 = {0}\n", p1.TransformBy(Matrix3d.WorldToPlane(yzPlane)));
                ed.WriteMessage("p2 = {0}\n", p2.TransformBy(Matrix3d.WorldToPlane(yzPlane)));
                ed.WriteMessage("p3 = {0}\n", p3.TransformBy(Matrix3d.WorldToPlane(zxPlane)));
                ed.WriteMessage("p4 = {0}\n", p4.TransformBy(Matrix3d.WorldToPlane(zxPlane)));
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
                ed.WriteMessage(ex.StackTrace);
            }
        }

        internal static AlignedDimension CreateAlignedDimension(Point3d p1, Point3d p2, double offset, Plane plane)
        {
            AlignedDimension dim = new AlignedDimension();
            // Transform WCS points to plane
            p1 = p1.TransformBy(Matrix3d.WorldToPlane(plane));
            p2 = p2.TransformBy(Matrix3d.WorldToPlane(plane));
            dim.XLine1Point = p1;
            dim.XLine2Point = p2;

            using (Line line = new Line(p1, p2))
            {
                using (Curve c = line.GetOffsetCurves(offset)[0] as Curve)
                {
                    dim.DimLinePoint = c.GetPointAtParameter(p1.DistanceTo(p2) / 2);
                }
            }
            // return transformed Dimension to WCS
            dim.TransformBy(Matrix3d.PlaneToWorld(plane));
            return dim;
        }

        internal static ObjectId AddToModelSpace(this Entity ent)
        {
            ObjectId objId = ObjectId.Null;
            if (ent == null)
            {
                throw new ArgumentNullException();
            }
            Database Db = HostApplicationServices.WorkingDatabase;
            Autodesk.AutoCAD.DatabaseServices.TransactionManager Tm = Db.TransactionManager;
            using (Transaction tr = Tm.StartTransaction())
            {
                BlockTable tb = (BlockTable)tr.GetObject
                  (Db.BlockTableId, OpenMode.ForRead, false);
                objId = ((BlockTableRecord)tr.GetObject
                  (tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false)).AppendEntity(ent);
                tr.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }
    }
}
« Last Edit: February 14, 2009, 06:08:49 AM by gile »
Speaking English as a French Frog

bikelink

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #8 on: February 14, 2009, 06:35:11 AM »
Amazing gile ! I haven't words to say how much precious this suggest was for me!
Thank You very very much!    
I would not have ever been able to made this on my own!


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #9 on: February 14, 2009, 07:56:55 AM »
You're welcome, I'm learning too...

As I thaught, there's a simpler way, just using the plane Normal and use The Elevation property of the Dimension class. The dimension elevation is gotten with the Z coordinate of a transformed point.

Code: [Select]
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace ExecMethod
{
    public static class Commands
    {
        [CommandMethod("doit")]
        public static void doit()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            try
            {
                Point3d p1 = new Point3d(0, 0, 0);
                Point3d p2 = new Point3d(0, 100, 100);
                // YZ plane normal
                Vector3d yzNormal = new Vector3d(1, 0, 0);

                Point3d p3 = new Point3d(10, 20, 30);
                Point3d p4 = new Point3d(60, 20, 50);
                // ZX plane normal
                Vector3d zxNormal = new Vector3d(0, -1, 0);

                AlignedDimension Dim1 = CreateAlignedDimension(p1, p2, 10.0, yzNormal);
                AlignedDimension Dim2 = CreateAlignedDimension(p3, p4, 10.0, zxNormal);

                Dim1.AddToModelSpace();
                Dim2.AddToModelSpace();

                ed.WriteMessage("Transformed p1 = {0}\n", p1.TransformBy(Matrix3d.WorldToPlane(yzNormal)));
                ed.WriteMessage("Transformed p2 = {0}\n", p2.TransformBy(Matrix3d.WorldToPlane(yzNormal)));
                ed.WriteMessage("Transformed p3 = {0}\n", p3.TransformBy(Matrix3d.WorldToPlane(zxNormal)));
                ed.WriteMessage("Transformed p4 = {0}\n", p4.TransformBy(Matrix3d.WorldToPlane(zxNormal)));
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message);
                ed.WriteMessage(ex.StackTrace);
            }
        }

        internal static AlignedDimension CreateAlignedDimension(Point3d p1, Point3d p2, double offset, Vector3d normal)
        {
            AlignedDimension dim = new AlignedDimension();
            // Transform WCS points
            p1 = p1.TransformBy(Matrix3d.WorldToPlane(normal));
            p2 = p2.TransformBy(Matrix3d.WorldToPlane(normal));

            dim.XLine1Point = p1;
            dim.XLine2Point = p2;

            using (Line line = new Line(p1, p2))
            {
                using (Curve c = line.GetOffsetCurves(offset)[0] as Curve)
                {
                    dim.DimLinePoint = c.GetPointAtParameter(p1.DistanceTo(p2) / 2);
                }
            }
            // return transformed Dimension to WCS
            dim.TransformBy(Matrix3d.PlaneToWorld(normal));
            dim.Elevation = p1.Z;
            return dim;
        }

        internal static ObjectId AddToModelSpace(this Entity ent)
        {
            ObjectId objId = ObjectId.Null;
            if (ent == null)
            {
                throw new ArgumentNullException();
            }
            Database Db = HostApplicationServices.WorkingDatabase;
            Autodesk.AutoCAD.DatabaseServices.TransactionManager Tm = Db.TransactionManager;
            using (Transaction tr = Tm.StartTransaction())
            {
                BlockTable tb = (BlockTable)tr.GetObject
                  (Db.BlockTableId, OpenMode.ForRead, false);
                objId = ((BlockTableRecord)tr.GetObject
                  (tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false)).AppendEntity(ent);
                tr.AddNewlyCreatedDBObject(ent, true);
                tr.Commit();
            }
            return objId;
        }
    }
}
Speaking English as a French Frog

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8740
  • AKA Daniel
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #10 on: February 14, 2009, 08:24:25 AM »
Nice Work Gile  8-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #11 on: February 14, 2009, 08:34:33 AM »
Thanks Daniel, you did most of the work.
I just played a little with transfomations...
Speaking English as a French Frog

bikelink

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #12 on: February 17, 2009, 03:04:36 AM »
of course also the help from Daniel was precious.. but the dimension on plan xy was already able to make it.

James Cannon

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #13 on: February 17, 2009, 08:17:29 AM »
This may be a dumb question, but why can't you just change your UCS dynamically and avoid the overhead of programmatic solutions altogether?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #14 on: February 17, 2009, 08:36:47 AM »
With all due respect, on this forum that's akin to asking a skier "why bother skiing down a hill when there's a perfectly good gondola?". :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

James Cannon

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #15 on: February 17, 2009, 08:47:29 AM »
With all due respect, on this forum that's akin to asking a skier "why bother skiing down a hill when there's a perfectly good gondola?". :)

I understand that notion, but reading through here, I couldn't tell if this was a programming of passion, or a programming of necessity, and figured I'd make sure that "d'oh" moment of "forest for the trees" was deliberately passed, and not just an oversight.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #16 on: February 17, 2009, 09:25:50 AM »
Often it's a passion that sports a side effect of fulfilling an actual requirement, present or future. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8740
  • AKA Daniel
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #17 on: February 17, 2009, 10:19:48 AM »
This may be a dumb question, but why can't you just change your UCS dynamically and avoid the overhead of programmatic solutions altogether?

I think it’s because the points are in WCS, does that make sense?

bikelink

  • Guest
Re: How to create a Dimension on plan XZ or YZ ?
« Reply #18 on: February 17, 2009, 12:41:39 PM »
I have tried to make a dimension with ucs aligned without take a right object..
maybe 'cause i haven't really understood the elevation property.
In docs autodesk talks about dimension with all WCS points and also Dimlinepoint in WCS...
so I thought give at the object 2 points (start - end ) and the third for the dimline. I've aligned the ucs at my line..but the dimension was offset from it..
I appreciated very much the help, and I usally don't look for the code ready , i try instead to understood my mistakes.
And probably i've been used a gondola instead of ski. (maybe 'cause Venice is really close from here.)