Projection AutoCad NET

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

AcadNetAddinWizard & More

Happy New Year


Happy New Year!
Dec 31, 2013 9:36:43 PM
Like 1

AcadNetAddinWizard & More


VARIOUS ADDIN/API WIZARDS, CODERS, WIDGETS AND EXAMPLES ARE PROVIDED TO

Comment

Reblog It

Spiderinnet1
a spider crawling mainly in the .NET of AutoCAD

Recreate AutoCAD Nested Entity with .NET PromptNestedEntityResult and Transform Matrix3d
Many times, we need to select a nested entity inside a block, for example, to perform some operations such as querying information and making modifications. In this article, let us see how to select such a nested entity and recreate it into the model space through some simple but proper transformations.
[CommandMethod("MoveNestedEntity")] public static void MoveNestedEntity_Method() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { PromptNestedEntityOptions nestedEntOpt = new PromptNestedEntityOptions("\nPick a nested entity:"); PromptNestedEntityResult nestedEntRes = ed.GetNestedEntity(nestedEntOpt); if (nestedEntRes.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Entity ent = tr.GetObject(nestedEntRes.ObjectId, OpenMode.ForRead) as Entity; Entity newEnt = ent.Clone() as Entity; newEnt.TransformBy(nestedEntRes.Transform.PostMultiplyBy(Matrix3d.Displacement(new Vector3d(0.1, 0.1, 0)))); btr.AppendEntity(newEnt); tr.AddNewlyCreatedDBObject(newEnt, true); tr.Commit(); } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

2
FOLLOWING

16
FOLLOWERS

Follow

Search
Search

Recent Comments
Spiderinnet1: Patrick, you are welcome.
ON AUTOCAD .NET DBOBJECT EVENTS LAYERS AS SAMPLES

Patrick: Thank you very much for your advices.


ON AUTOCAD .NET DBOBJECT EVENTS LAYERS AS SAMPLES

Spiderinnet1: Your way should just work and looks the most ef... | more
ON AUTOCAD .NET DBOBJECT EVENTS LAYERS AS SAMPLES

If we run the command and select a text in a block nested some levels away, we may see the recreated text along with the block reference as follows.

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

As can be seen, the Transform property of the Matrix3d type of the PromptNestedEntityResult can be used directly to do the geometry transformation from the nested block level regardless of how deep it may be of the selected nested entity to the outmost model space. The better part is that the UCS has already been properly taken into account. Therefore, we do not have to bother to iterate through all those block containers to get their matrix information and do many unnecessary and error prone transformations in this simple case. By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Dec 30, 2013 9:31:20 PM
Like 0

Comment

Reblog It

AutoCAD .NET PromptNestedEntityOptions & PromptNestedEntityResult & Editor.GetNestedEntity


Many times, we need to select a nested entity inside a block, for example, to perform some operations such as querying information and making modifications. In this article, let us see how to select such a nested entity, query its information, and iterate through its containers from inside to outmost.
[CommandMethod("SelectNestedEntity")] public static void SelectNestedEntity_Method() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { PromptNestedEntityOptions nestedEntOpt = new PromptNestedEntityOptions("\nPick a nested entity:"); PromptNestedEntityResult nestedEntRes = ed.GetNestedEntity(nestedEntOpt); if (nestedEntRes.Status == PromptStatus.OK) { using (Transaction tr = db.TransactionManager.StartTransaction())

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More


{ int level = 0; Entity ent = tr.GetObject(nestedEntRes.ObjectId, OpenMode.ForRead) as Entity; ed.WriteMessage(string.Format("\nType of the nested entity: {0}", ent.ToString())); ed.WriteMessage(string.Format("\n#{0} level - block name: {1}", ++level, ent.BlockName)); foreach (ObjectId id in nestedEntRes.GetContainers()) { Entity container = tr.GetObject(id, OpenMode.ForRead) as Entity; ed.WriteMessage(string.Format("\n#{0} level - block name: {1}", ++level, container.BlockName)); } tr.Commit(); } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

If we run the command and select a text in a block nested five levels away, the following may be printed out to the AutoCAD command line window.
Command: SelectNestedEntity Pick a nested entity: Type of the nested entity: Autodesk.AutoCAD.DatabaseServices.DBText #1 level - block name: block1 #2 level - block name: block12 #3 level - block name: block123 #4 level - block name: block1234 #5 level - block name: block12345 #6 level - block name: *Model_Space

If a dimension text is selected instead, the following may be printed out this time.
Command: SELECTNESTEDENTITY Pick a nested entity: Type of the nested entity: Autodesk.AutoCAD.DatabaseServices.MText #1 level - block name: *D12 #2 level - block name: *Model_Space

By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Dec 24, 2013 12:55:37 AM
Like 0

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations Project Entity to Plane


Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. We demonstrated projecting a vector to a plane with a few matrix and transformation methods

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

such as Matrix3d.Projection, TransformBy, CrossProduct, and Matrix3d.AlignCoordinateSystem before. We also demonstrated projecting a point onto a plane along the normal of the plane or along a different direction using a similar approach. In this post, lets try to project AutoCAD entities to a plane. We are going to project a picked entity onto the same Plane (whose center is at (0.5, 0.5, 0) and normal (1, 1, 1)) this time.
[CommandMethod("Matrix_ProjectEntOntoPlane")] public static void Matrix_ProjectEntOntoPlane_Method() { Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Database db = HostApplicationServices.WorkingDatabase; PromptEntityResult prEntRes1 = ed.GetEntity("Select an entity to project to the Plane at origin(0.5,0.5,0) with normal (1,1,1): "); if (prEntRes1.Status == PromptStatus.OK) { using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Entity ent = (Entity)trans.GetObject(prEntRes1.ObjectId, OpenMode.ForRead); Entity newEnt = (Entity)ent.Clone(); Point3d originOfPlane = new Point3d(0.5, 0.5, 0); Vector3d normalOfPlane = (new Vector3d(1, 1, 1)).GetNormal(); Plane plane = new Plane(originOfPlane, normalOfPlane); Matrix3d mat = Matrix3d.Projection(plane, normalOfPlane); newEnt.TransformBy(mat); btr.AppendEntity(newEnt); trans.AddNewlyCreatedDBObject(newEnt, true); trans.Commit(); } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

If we run the command, the following will be printed out to the AutoCAD command line window.
Command: POINT Current point modes: PDMODE=98 PDSIZE=0.0000 Specify a point: 0,0,0 Command: MATRIX_PROJECTENTONTOPLANE Select an entity to project to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last Command: POINT Current point modes: PDMODE=98 PDSIZE=0.0000 Specify a point: 0.2,0.1,0 Command: MATRIX_PROJECTENTONTOPLANE Select an entity to project to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last Command: L LINE Specify first point: 0,0,0 Specify next point or [Undo]: 0.2,0.2,0 Specify next point or [Undo]: Command: MATRIX_PROJECTENTONTOPLANE Select an entity to project to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last Autodesk.AutoCAD.Runtime.Exception: eCannotScaleNonUniformly at Autodesk.AutoCAD.DatabaseServices.Entity.TransformBy(Matrix3d transform) at Command: CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 0.3,0.3,0 Specify radius of circle or [Diameter]: 0.2 Command: MATRIX_PROJECTENTONTOPLANE Select an entity to project to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More


Autodesk.AutoCAD.Runtime.Exception: eCannotScaleNonUniformly at Autodesk.AutoCAD.DatabaseServices.Entity.TransformBy(Matrix3d transform) at

As can be seen, Point entities can be successfully projected to a Plane, but Line and Circle cannot because non-uniform scaling is not supported in AutoCAD in this regard, as the eCannotScaleNonUniformly exception message indicates. The test drawing may look like this.

By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Dec 11, 2013 9:38:45 PM
Like 0

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations Project Point to Plane Pt.2


Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. We demonstrated projecting a vector to a plane with a few matrix and transformation methods

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

such as Matrix3d.Projection, TransformBy, CrossProduct, and Matrix3d.AlignCoordinateSystem before. We also demonstrated projecting a point onto a plane along the normal of the plane using a similar approach. In fact, the same point can be projected onto the same plane along different directions as the second argument of the Matrix3d.Projection() method indicates. In this post, lets address this scenario. We are going to project the same point (7, 8, 9) onto the same Plane (whose center is at (0.5, 0.5, 0) and normal (1, 1, 1)) but along the world Z Axis (e.g. (0, 0, 1)) direction this time.
[CommandMethod("Matrix_ProjectPointOntoPlane2")] public static void Matrix_ProjectPointOntoPlane2_Method() { Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Point3d origin2 = new Point3d(0.5, 0.5, 0); Vector3d normal2 = (new Vector3d(1, 1, 1)).GetNormal(); Plane plane2 = new Plane(origin2, normal2); Matrix3d prjMat2 = Matrix3d.Projection(plane2, Vector3d.ZAxis); Point3d pt2 = new Point3d(7, 8, 9); Point3d prjPt2 = pt2.TransformBy(prjMat2); ed.WriteMessage("\nProject (7,8,9) along ZAxis onto plane at (0.5,0.5,0) with normal (1,1,1): {0}", prjPt2); Line3d geoLine = new Line3d(pt2, Vector3d.ZAxis); Point3d[] intPts = plane2.IntersectWith(geoLine); ed.WriteMessage("\nCalculated result: {0}", intPts[0]); } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

If we run the command, the following will be printed out to the AutoCAD command line window.
Command: Matrix_ProjectPointOntoPlane2 Project (7,8,9) along ZAxis onto plane at (0.5,0.5,0) with normal (1,1,1): (7,8,-14) Calculated result: (7,8,-14)

As can be seen, the projected point using the Matrix3d.Projection and TransformBy approach reported the same values as we calculated out in another way. By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Dec 10, 2013 9:31:07 PM
Like 0

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations PlaneToWorld


Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. We demonstrated various aspects about the Matrix3d.PlaneToWorld () method and what it really gives us in some early posts. In this article, lets talk about its inverse, Matrix3d.PlaneToWorld (). Just as the Matrix3d.WorldToPlane() can easily build up a transformation matrix from the world space (WCS) to a particular plane space which can be defined by a point and a vector. The Matrix3d.PlaneToWorld () can perform the opposite work, creating a transformation matrix from a particular plane space which can be defined by a point and a vector to the world space (WCS). A plane whose origin is (0.5, 0.5, 0) and normal (1, 1, 1) will be used for demonstration purpose in the coming code and command.

Now the test code and command:


[CommandMethod("Matrix_PtPlaneToWorld")] public static void Matrix_PtPlaneToWorld() { Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Database db = HostApplicationServices.WorkingDatabase; PromptPointResult prReslt = ed.GetPoint("Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): "); if (prReslt.Status == PromptStatus.OK) { using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Point3d ptOnPlane = prReslt.Value.TransformBy(Matrix3d.WorldToPlane(new Plane(new Point3d(0.5, 0.5, 0), new Vector3d(1, 1, 1)))); ed.WriteMessage(string.Format("\nPoint {0} on the plane has {1} in WCS.", prReslt.Value, ptOnPlane)); trans.Commit(); } } }

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More


catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

Finally, here are some test results for people to check:


Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0,0,0 Point (0,0,0) on the plane has (0.5,0.5,0) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0,0.408248290463863,-0.577350269189626 Point (0,0.408248290463863,-0.577350269189626) on the plane has (-1.11022302462516E-16,-1.11022302462516E-16,-1.11022302462516E-16) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0,0.408248290463863,1.15470053837925 Point (0,0.408248290463863,1.15470053837925) on the plane has (0.999999999999999,0.999999999999999,0.999999999999999) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): -0.707106781186548,0,0 Point (-0.707106781186548,0,0) on the plane has (1,-3.33066907387547E-16,0) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0.707106781186548,0,0 Point (0.707106781186548,0,0) on the plane has (-3.33066907387547E-16,1,0) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0,-0.408248290463863,0.577350269189626 Point (0,-0.408248290463863,0.577350269189626) on the plane has (1,1,1.11022302462516E-16) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0.707106781186547,-0.612372435695795,2.1650635094611 Point (0.707106781186547,-0.612372435695795,2.1650635094611) on the plane has (1.5,2.5,0.750000000000001) in WCS. Command: MATRIX_PTPLANETOWORLD Input a point on the Plane at origin(0.5,0.5,0) with normal (1,1,1): 0.707106781186548,1.63299316185545,1.58771324027147 Point (0.707106781186548,1.63299316185545,1.58771324027147) on the plane has (0.25,1.25,2.25) in WCS.

By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Dec 5, 2013 10:44:27 PM
Like 0

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations Project


http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

Point to Plane
Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. We demonstrated projecting a vector to a plane with a few matrix and transformation methods such as Matrix3d.Projection, TransformBy, CrossProduct, and Matrix3d.AlignCoordinateSystem before. In this post, lets use a similar approach to project points onto a plane.
[CommandMethod("Matrix_ProjectPointOntoPlane")] public static void Matrix_ProjectPointOntoPlane_Method() { Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Point3d origin1 = Point3d.Origin; Vector3d normal1 = Vector3d.XAxis; Plane plane1 = new Plane(origin1, normal1); Matrix3d prjMat1 = Matrix3d.Projection(plane1, plane1.Normal); Point3d pt1 = new Point3d(3, 4, 5); Point3d prjPt1 = pt1.TransformBy(prjMat1); ed.WriteMessage("\nProjected from (3,4,5) onto YZ plane: {0}", prjPt1); Point3d origin2 = new Point3d(0.5, 0.5, 0); Vector3d normal2 = (new Vector3d(1, 1, 1)).GetNormal(); Plane plane2 = new Plane(origin2, normal2); Matrix3d prjMat2 = Matrix3d.Projection(plane2, plane2.Normal); Point3d pt2 = new Point3d(7, 8, 9); Point3d prjPt2 = pt2.TransformBy(prjMat2); ed.WriteMessage("\nProjected from (7,8,9) onto plane at (0.5,0.5,0) with normal (1,1,1): {0}", prjPt2); Line3d geoLine = new Line3d(pt2, normal2); double dist = geoLine.GetDistanceTo(origin2); CircularArc3d geoCir = new CircularArc3d(origin2, normal2, dist); Point3d[] intPts = geoCir.IntersectWith(geoLine); ed.WriteMessage("\nCalculated from (7,8,9) onto plane at (0.5,0.5,0) with normal (1,1,1): {0}", intPts[0]); } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } } }

If we run the command, the following will be printed out to the AutoCAD command line window.
Command: Matrix_ProjectPointOntoPlane Projected from (3,4,5) onto YZ plane: (0,4,5) Projected from (7,8,9) onto plane at (0.5,0.5,0) with normal (1,1,1): (-0.666666666666668,0.333333333333332,1.33333333333333) Calculated from (7,8,9) onto plane at (0.5,0.5,0) with normal (1,1,1): (-0.666666666666667,0.333333333333333,1.33333333333333)

As can be seen, the projected points using the Matrix3d.Projection and TransformBy approach reported the same values as we commonly felt and calculated out in another way. By the way, the test C# project along with most code of the test command and the transaction

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.

Dec 1, 2013 6:52:51 PM

Like

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations Commonly Transform From World To Plane
Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. The AutoCAD Matrix3d class provides a handy method WorldToPlane() to easily build up a transformation matrix from the world space (WCS) to a particular plane space which can be defined by a point and a vector. The method looks straightforward to use and it does not seem to have many indications, but the fact is not so at all. The most important is that, as mentioned before, the matrix created by the WorldToPlane() method should apply to Points instead of Entities that other matrixes generally aim at. We demonstrated before that the entities being transformed by the WorldToPlane matrix are not as expected at all. Instead of staying on the plane, the transformed entities will be located at some weird places. A bit earlier, we also created a proper matrix, which applies to entity transformation instead, with the method Matrix3d.AlignCoordinateSystem instead of the Matrix3d.WorldToPlane. However, it hard coded all the origin point, X vector, Y vector and Z vector there to simply things before another method Matrix3d.Projection was demonstrated in the previous post. In this post, lets remove the limitation and create a common method which applies to all Planes. Here is the code and command.
[CommandMethod("Matrix_WorldToPlane3")] public static void Matrix_WorldToPlane3_Method() { Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Database db = HostApplicationServices.WorkingDatabase; PromptEntityResult prEntRes1 = ed.GetEntity("Select an entity in World to copy to the Plane at origin(0.5,0.5,0) with normal (1,1,1): "); if (prEntRes1.Status == PromptStatus.OK) { using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Entity ent = (Entity)trans.GetObject(prEntRes1.ObjectId, OpenMode.ForRead); Entity newEnt = (Entity)ent.Clone(); Point3d originOfPlane = new Point3d(0.5, 0.5, 0); Vector3d normalOfPlane = (new Vector3d(1, 1, 1)).GetNormal(); Plane plane = new Plane(originOfPlane, normalOfPlane); Matrix3d mat = WorldToPlaneTransformMatrix(plane);

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More


newEnt.TransformBy(mat); btr.AppendEntity(newEnt); trans.AddNewlyCreatedDBObject(newEnt, true); trans.Commit(); } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } } public static Matrix3d WorldToPlaneTransformMatrix(Plane plane1) { Point3d origin1 = plane1.PointOnPlane; Matrix3d prjMat1 = Matrix3d.Projection(plane1, plane1.Normal); Vector3d zOnPlane = plane1.Normal; Vector3d yOnPlane = Vector3d.ZAxis.TransformBy(prjMat1).GetNormal(); Vector3d xOnPlane = yOnPlane.CrossProduct(zOnPlane).GetNormal(); Matrix3d mat = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, origin1, xOnPlane, yOnPlane, zOnPlane); return mat; }

If we use the same test data and entities to test the command,
Command: C CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 0,0.3 Specify radius of circle or [Diameter]: 0.3 Command: MATRIX_WORLDTOPLANE2 Select an entity in World to copy to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last Command: DIM Dim: dia Select arc or circle: Enter dimension text <0.6000>: Specify dimension line location or [Mtext/Text/Angle]: -0.3,0.3,0 Dim: exit Command: MATRIX_WORLDTOPLANE2 Select an entity in World to copy to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last

we get the same drawing as before as follows.

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

As can be seen, circle and the dimension are properly transformed from the World (WCS) to the Plane space. By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Nov 27, 2013 3:25:08 PM
Like 0

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations Matrix.Projection


Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. The AutoCAD Matrix3d class provides a handy method Project() to easily project geometries such as a vector like the Z Axis from the world space (WCS) to a particular plane space which can be defined by a point such as (0.5,0.5,0) and a vector such as (1,1,1). In this post, lets demonstrate so.
[CommandMethod("Matrix_Projection")] public static void Matrix_Projection_Method()

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More


{ Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Point3d origin1 = new Point3d(0.5, 0.5, 0); Vector3d normal1 = (new Vector3d(1, 1, 1)).GetNormal(); Plane plane1 = new Plane(origin1, normal1); Matrix3d prjMat1 = Matrix3d.Projection(plane1, plane1.Normal); Vector3d yProjection = Vector3d.ZAxis.TransformBy(prjMat1).GetNormal(); Vector3d yPlane1 = origin1.GetVectorTo(new Point3d(0, 0, 1)).GetNormal(); ed.WriteMessage("The vector projected from World Z Axis to the Plane: {0}\n", yProjection); ed.WriteMessage("The vector calculated by two points on the Plane: {0}\n", yPlane1); } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

Here is the command output:


Command: Matrix_Projection The vector projected from World Z Axis to the Plane: (-0.408248290463863,-0.408248290463863,0.816496580927726) The vector calculated by two points on the Plane: (-0.408248290463863,-0.408248290463863,0.816496580927726)

As can be seen, the projection result equals to our calculation result. Here are the spaces and axises (vectors) in the drawing for reference.

By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Nov 19, 2013 10:33:41 PM
Like 0

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

Comment

Reblog It

AutoCAD .NET: Matrix & Transformations Transform From World To Plane


Matrix and Transformations are very important in AutoCAD and any other CAD (Computer Aided/Assisted Design/Drawing) systems. Without knowing them theoretically well or using practically properly, simple tasks can become unbelievably difficult, and the advanced CAD systems will be underused just like some kid drawing tools such as the Paint Brush. Spending some time on being familiar with them, we can save many efforts and at the same time make our applications behave more professionally. We have been presenting a series of articles regarding matrix and transformations. AutoCAD will be used for demonstration and practice purposes, but most of the methodologies will also apply to any other CAD systems such as Inventor, Revit, 3D Max, Microstation, and Solid Work. The AutoCAD Matrix3d class provides a handy method WorldToPlane() to easily build up a transformation matrix from the world space (WCS) to a particular plane space which can be defined by a point and a vector. The method looks straightforward to use and it does not seem to have many indications, but the fact is not so at all. The most important is that, as mentioned in the previous post, the matrix created by the WorldToPlane() method should apply to Points instead of Entities that other matrixes generally aim at. We demonstrated previously that the entities being transformed by the WorldToPlane matrix are not as expected at all. Instead of staying on the plane, the transformed entities will be located at some weird places. In this post, lets see how to really transform entities from World to Plane. The key point is to use the matrix created by the Matrix3d.AlignCoordinateSystem method instead of the Matrix3d.WorldToPlane. Here is the test code and command first:
[CommandMethod("Matrix_WorldToPlane2")] public static void Matrix_WorldToPlane2_Method() { Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor; try { Database db = HostApplicationServices.WorkingDatabase; PromptEntityResult prEntRes1 = ed.GetEntity("Select an entity in World to copy to the Plane at origin(0.5,0.5,0) with normal (1,1,1): "); if (prEntRes1.Status == PromptStatus.OK) { using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); Entity ent = (Entity)trans.GetObject(prEntRes1.ObjectId, OpenMode.ForRead); Entity newEnt = (Entity)ent.Clone(); Point3d originPlane = new Point3d(0.5, 0.5, 0); Vector3d zPlane = (new Vector3d(1,1,1)).GetNormal(); Vector3d yPlane = originPlane.GetVectorTo(new Point3d(0,0,1)).GetNormal(); Vector3d xPlane = yPlane.CrossProduct(zPlane).GetNormal(); Matrix3d mat = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, originPlane, xPlane, yPlane, zPlane); newEnt.TransformBy(mat); btr.AppendEntity(newEnt); trans.AddNewlyCreatedDBObject(newEnt, true); trans.Commit(); }

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More


} } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } }

Here is what we are going to do in an AutoCAD session with the test assembly loaded and the previous test model opened:
Command: C CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 0,0.3 Specify radius of circle or [Diameter]: 0.3 Command: MATRIX_WORLDTOPLANE2 Select an entity in World to copy to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last Command: DIM Dim: dia Select arc or circle: Enter dimension text <0.6000>: Specify dimension line location or [Mtext/Text/Angle]: -0.3,0.3,0 Dim: exit Command: MATRIX_WORLDTOPLANE2 Select an entity in World to copy to the Plane at origin(0.5,0.5,0) with normal (1,1,1): last

Now we get the following drawing:

As can be seen, circle and the dimension are properly transformed from the World (WCS) to the Plane space. By the way, the test C# project along with most code of the test command and the transaction starter code were all automatically created by AutoCAD .NET Addin Wizard (AcadNetAddinWizard), more specifically, its AutoCAD .NET Addin project wizard, Local Commands item wizard, and Entity Appender coder.
Nov 18, 2013 11:09:42 PM
Like 0

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

AcadNetAddinWizard & More

Comment

Reblog It

Next

Subscribe to this blog's feed

Powered by Typepad

http://spiderinnet1.typepad.com/blog/[08/01/2014 09:13:55]

You might also like