@Alexander.Rivilis wrote:
Example: http://adndevblog.typepad.com/autocad/2012/04/finding-transformation-matrix-for-aligning-two-entitie...
Hi Alexander,
I have try your code. But it align perpendicularly, and also it will align entity size (when possible).
I'm trying to do it by making a mirror line in the middle of 1st origin and destination and 2nd origin and destination point.
The problem is if origins and destinations already in parallel, the code can't align to the opposite.
Here is my code:
<CommandMethod("tal")> _
Public Sub TestAlignment()
Dim doc As Document = AcApp.DocumentManager.MdiActiveDocument
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim ed As Editor = doc.Editor
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim per As PromptEntityResult = ed.GetEntity(vbLf & "Select block to align")
Dim br As BlockReference = tr.GetObject(per.ObjectId, OpenMode.ForWrite)
'select point origin
Dim ppr As PromptPointResult = ed.GetPoint(vbLf & "Select 1 Origin")
Dim pt1 As Point3d = ppr.Value
'This will be origin point
Dim ptO As Point3d = pt1
ppr = ed.GetPoint(vbLf & "Select 2 Origin")
Dim pt2 As Point3d = ppr.Value
'select point destination
ppr = ed.GetPoint(vbLf & "Select 1 dest")
Dim pt3 As Point3d = ppr.Value
ppr = ed.GetPoint(vbLf & "Select 2 dest")
Dim pt4 As Point3d = ppr.Value
'This will be destination point
Dim ptD As Point3d = pt4
'Create temporary line for origin and destination points
Dim l1 As Line = New Line(pt1, pt2)
Dim l2 As Line = New Line(pt3, pt4)
'Make sure distance between origin and destination points are same
If l2.Length > l1.Length Then
pt4 = l2.GetPointAtDist(l1.Length)
l2.EndPoint = pt4
ElseIf l2.Length < l1.Length Then
pt2 = l1.GetPointAtDist(l2.Length)
l1.EndPoint = pt2
End If
'Find middle point of 1st & 2nd origin and destination
Dim m1 As Point3d = New Point3d((pt1.X + pt3.X) * 0.5, (pt1.Y + pt3.Y) * 0.5, (pt1.Z + pt3.Z) * 0.5)
Dim m2 As Point3d = New Point3d((pt2.X + pt4.X) * 0.5, (pt2.Y + pt4.Y) * 0.5, (pt2.Z + pt4.Z) * 0.5)
'Create temporary line for mirroring purpose
Dim l5 As Line3d = New Line3d(m1, m2)
br.TransformBy(Matrix3d.Mirroring(l5))
br.TransformBy(Matrix3d.Displacement(br.Position.GetVectorTo(ptD)))
'br.Rotation = br.Rotation * -1
tr.Commit()
End Using
End Sub
regards
Abufaisal