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)
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;
}
}
}