Insert block and matrix transform it to p1 and p2 possition problem...

Insert block and matrix transform it to p1 and p2 possition problem...

jokiller70
Advocate Advocate
953 Views
5 Replies
Message 1 of 6

Insert block and matrix transform it to p1 and p2 possition problem...

jokiller70
Advocate
Advocate

I want to insert a block and rotate it to 2 points that i select in modelspace. But i get eCannotScaleNonUniformly with this code;

 

                        Dim acPt3d As Point3d = New Point3d(0, 0, 0)
                        Dim acVec3d As Vector3d = acPt3d.GetVectorTo(p1)
                        blockRef.TransformBy(Matrix3d.Displacement(acVec3d))
                        Dim zAxis As Vector3d = curUCS.Zaxis
                        Dim xAxis As Vector3d = p1.GetVectorTo(p2).GetNormal
                        Dim yAxis As Vector3d
                        If xAxis.IsEqualTo(zAxis) Then
                            yAxis = curUCS.Yaxis
                            zAxis = xAxis.CrossProduct(yAxis).GetNormal
                        Else
                            yAxis = zAxis.CrossProduct(xAxis).GetNormal
                        End If
                        blockRef.TransformBy(New Matrix3d(New Double() {xAxis.X, yAxis.X, zAxis.X, p1.X, xAxis.Y, yAxis.Y, zAxis.Y, p1.Y, xAxis.Z, yAxis.Z, zAxis.Z, p1.Z, 0, 0, 0, 1}))
 
0 Likes
954 Views
5 Replies
Replies (5)
Message 2 of 6

Matti72
Advocate
Advocate

Your Matrix probably contains a scale factor (not all Vectors seems to be normals)...

 

Here should be a solution for your problem.

 

http://adndevblog.typepad.com/autocad/2012/08/remove-scaling-from-transformation-matrix.html

0 Likes
Message 3 of 6

jokiller70
Advocate
Advocate

Still same problem...

 

Dim mx As Matrix3d = New Matrix3d(New Double() {xAxis.X, yAxis.X, zAxis.X, p1.X, xAxis.Y, yAxis.Y, zAxis.Y, p1.Y, xAxis.Z, yAxis.Z, zAxis.Z, p1.Z, 0, 0, 0, 1})
                        Dim axes As New List(Of Vector3d)

                        For i As Integer = 0 To 2
                            Dim vec As New Vector3d(mx(i, 0), mx(i, 1), mx(i, 2))
                            vec = vec.GetNormal()
                            axes.Add(vec)
                        Next

                        Dim mx2 As Matrix3d = New Matrix3d(New Double() _
                        {
                          axes(0).X, axes(0).Y, axes(0).Z, mx(0, 3),
                          axes(1).X, axes(1).Y, axes(1).Z, mx(1, 3),
                          axes(2).X, axes(2).Y, axes(2).Z, mx(2, 3),
                          mx(3, 0), mx(3, 1), mx(3, 2), mx(3, 3)
                        })

                        blockRef.TransformBy(mx2)
0 Likes
Message 4 of 6

augusto.goncalves
Alumni
Alumni
from your initial description and this last code, I'm not sure what you want to achieve here...

can you try add an image to this thread showing what you want?
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 5 of 6

jokiller70
Advocate
Advocate

I want to insert a block(almost only a straight line) at p1 and rotate/transform it so it points to p2.

 

In VB I set a new ucs between the points and inserted the block.

 

Untitled-1.png

0 Likes
Message 6 of 6

_gile
Consultant
Consultant

Hi,

 

IMO, the most easier way is to define your block as a line along Z axis (e.g. from 0,0,0 to 0,0,1) and then, just use a plane which origin is the base point and the normal the vector from the base point to the second one to transform the block.

 

        [CommandMethod("test")]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptPointOptions("\nBase point: ");
            var result = ed.GetPoint(options);
            if (result.Status != PromptStatus.OK)
                return;
            var basePt = result.Value;

            options.Message = "\nDirection point: ";
            options.BasePoint = basePt;
            options.UseBasePoint = true;
            result = ed.GetPoint(options);
            if (result.Status != PromptStatus.OK)
                return;
            var dirPt = result.Value;

            var ucs = ed.CurrentUserCoordinateSystem;
            basePt = basePt.TransformBy(ucs);
            dirPt = dirPt.TransformBy(ucs);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 

                var plane = new Plane(basePt, basePt.GetVectorTo(dirPt));

                var br = new BlockReference(Point3d.Origin, bt["test"]);
                br.TransformBy(Matrix3d.PlaneToWorld(plane));
                curSpace.AppendEntity(br);
                tr.AddNewlyCreatedDBObject(br, true);
                tr.Commit();
            }
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes