VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Geometry of Rotated Dimension

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
1684 Views, 5 Replies

Geometry of Rotated Dimension

Hi There,

I have to find out the geometric Coordinates ( ExtLine1Point, ExtLine2Point, DimensionLine1 Point & DimensionLine2 Point ) of a Rotated Dimension using VBA. Is there any proven workaround available or will you suggest me how can i achieve this.

Thanks in Advance.

Regards
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: Anonymous

There are codes that allow you to use lisp to access the dxf codes for this. I use vbassoc as it is easy to follow. You would think they could have included the api by now, but no.
The points you want are defined in the *d block but you need dxf to find which *d block you need.
[code]
Option Explicit

Private VL
Function ParseDxfPoint(DxfPoint)

Dim Pt(2) As Double
Dim Gap1, Gap2
Gap1 = InStr(2, DxfPoint, " ", vbTextCompare)
Pt(0) = Mid(DxfPoint, 2, Gap1 - 1)
Gap2 = InStr(Gap1 + 1, DxfPoint, " ", vbTextCompare)
Pt(1) = Mid(DxfPoint, Gap1 + 1, Gap2 - (Gap1 + 1))
Pt(2) = Mid(DxfPoint, Gap2 + 1, Len(DxfPoint) - (Gap2 + 1))
ParseDxfPoint = Pt

End Function

Function GetVl() As Object

Dim VLisp As Object
Select Case AcadVer
Case Is < 2006
Set VLisp = ThisDrawing.Application.GetInterfaceObject("VL.Application.1")
Case 2006
Set VLisp = ThisDrawing.Application.GetInterfaceObject("VL.Application.16")
Case Else
Set VLisp = ThisDrawing.Application.GetInterfaceObject("VL.Application.16")
End Select

Set GetVl = VLisp

End Function



'SomeCallMeDave
'http://www.vbdesign.net/expresso/showthread.php?postid=83887#post83887
'Changed pAcadObj As AcadObject to pAcadObj As Object to access imagedef as well
'Modified by Jeff Mishler, March 2006, to get the Block table object, not Block_Record table object
Public Function vbAssoc(pAcadObj, pDXFCode As Integer) As Variant

Dim VLisp As Object
Dim VLispFunc As Object
Dim varRetVal As Variant

Dim obj1 As Object
Dim obj2 As Object

Dim strHnd As String
Dim strVer As String

Dim lngCount As Long
Dim i As Long
Dim j As Long

On Error GoTo vbAssocError

Set VLisp = GetVl


Set VLispFunc = VLisp.ActiveDocument.Functions

If Not TypeOf pAcadObj Is AcadBlock Then
strHnd = pAcadObj.Handle
Else
Dim lispStr As String
lispStr = "(cdr (assoc 5 (entget (tblobjname " & Chr(34) & "Block" & Chr(34) & Chr(34) & pAcadObj.Name & Chr(34) & "))))"
Set obj1 = VLispFunc.Item("read").Funcall(lispStr)
strHnd = VLispFunc.Item("eval").Funcall(obj1)
End If
Set obj1 = VLispFunc.Item("read").Funcall("pDXF")
varRetVal = VLispFunc.Item("set").Funcall(obj1, pDXFCode)
Set obj1 = VLispFunc.Item("read").Funcall("pHandle")
varRetVal = VLispFunc.Item("set").Funcall(obj1, strHnd)
Set obj1 = VLispFunc.Item("read").Funcall("(vl-princ-to-string (cdr (assoc pDXF (entget (handent pHandle)))))")
varRetVal = VLispFunc.Item("eval").Funcall(obj1)

vbAssoc = varRetVal

'clean up the newly created LISP symbols
Set obj1 = VLispFunc.Item("read").Funcall("(setq pDXF nil)")
varRetVal = VLispFunc.Item("eval").Funcall(obj1)
Set obj1 = VLispFunc.Item("read").Funcall("(setq pHandle nil)")
varRetVal = VLispFunc.Item("eval").Funcall(obj1)

'release the objects or Autocad gets squirrely (no offense RR)
Set obj2 = Nothing
Set obj1 = Nothing
Set VLispFunc = Nothing
Set VLisp = Nothing

Exit Function

vbAssocError:
Set obj2 = Nothing
Set obj1 = Nothing
Set VLispFunc = Nothing
Set VLisp = Nothing
MsgBox "Error occurred " & Err.Description

End Function



Public Function DimRotation(objDim As AcadDimension, varPick As Variant)

'code(10)=cross point rhs of text,arrow2
'code(11)= text insertpt
'code(13) =Dimstartpt-extendline1
'code(14) =DimEndpt-extendline2

Dim dblRot As Double
Dim dblStartToVarPickAng As Double, dblEndAng As Double
Dim dblEndToVarPickAng As Double
Dim startPt, endPt, arrow2Pt
Dim varTest As Variant, Ppt, x(1)
With ThisDrawing.Utility

'dblRot = vbAssoc(objDim, 50)

varTest = vbAssoc(objDim, 10)
arrow2Pt = ParseDxfPoint(varTest)

varTest = vbAssoc(objDim, 13)
startPt = ParseDxfPoint(varTest)

varTest = vbAssoc(objDim, 14)
endPt = ParseDxfPoint(varTest)

dblEndAng = .AngleFromXAxis(endPt, arrow2Pt)

x(1) = dblEndAng - 0.5 * pi

Dim dblDist As Double
dblDist = objDim.ExtensionLineExtend * objDim.ScaleFactor
Ppt = .PolarPoint(arrow2Pt, dblEndAng, dblDist)

dblStartToVarPickAng = .AngleFromXAxis(startPt, varPick)
If dblStartToVarPickAng > (2 * pi) - 0.001 Then
dblStartToVarPickAng = dblStartToVarPickAng - 2 * pi
End If

dblEndToVarPickAng = .AngleFromXAxis(endPt, varPick)
If dblEndToVarPickAng > (2 * pi) - 0.001 Then
dblEndToVarPickAng = dblEndToVarPickAng - 2 * pi
End If

If Abs(dblStartToVarPickAng - dblEndAng) _
< Abs(dblEndToVarPickAng - dblEndAng) Then

dblRot = dblEndAng + 0.5 * pi * isLeft(endPt, arrow2Pt, startPt) 'function
Ppt = .PolarPoint(Ppt, dblRot, objDim.Measurement)
End If

x(0) = Ppt
DimRotation = x

End With

[/code]
Message 3 of 6
Anonymous
in reply to: Anonymous

Hi Bryco,

Thanks for u r Reply. I tried this one and it is working but it is not giving exact points with Decimals, i mean it is rounding the Decimal values.

Regards,
Message 4 of 6
Anonymous
in reply to: Anonymous

It's a string so there are limitations.
Below is another way, slow though.
Try it on a standard cad dimension.
[code]
Sub DimR()

Dim B As AcadBlock
Dim Bs As AcadBlocks
Dim D As AcadDimRotated
Dim Tp As Variant, insPt As Variant
Dim Ent As AcadEntity
Dim i As Integer
Dim Fluff As Double

Fluff = 0.00000001
ThisDrawing.Utility.GetEntity D, Tp
Tp = D.TextPosition

Set Bs = ThisDrawing.Blocks
For Each B In Bs
If Not Left(B.Name, 2) = "*D" Then GoTo SkipBlock
For Each Ent In B
If TypeOf Ent Is AcadMText Then
Debug.Print Tp(0)
insPt = Ent.InsertionPoint
For i = 0 To 2
If Abs(insPt(i) - Tp(i)) > Fluff Then GoTo SkipBlock
Next i
ThisDrawing.ModelSpace.AddPoint B(8).Coordinates
ThisDrawing.ModelSpace.AddPoint B(9).Coordinates
ThisDrawing.ModelSpace.AddPoint B(7).Coordinates
Exit Sub
End If
Next Ent
SkipBlock:
Next B

End Sub
[/code]
Message 5 of 6
Anonymous
in reply to: Anonymous

Hi Bryco,

Thanks for u r reply. Here is the way to speed it up. This one uses GetSubEntity and the OwnerId to get the AcadBlock. Another SelectionSet is used to ensure that the entity at the pick point is an Rotated Dimension.

Sub DimR()

Dim b As AcadBlock
Dim Object As AcadObject
Dim PickedPoint As Variant, TransMatrix As Variant, ContextData As Variant
Dim HasContextData As String

' Get the sub entity and a pick point
ThisDrawing.Utility.GetSubEntity Object, PickedPoint, TransMatrix, ContextData
Debug.Print "Selected object is a " & Object.ObjectName

' Get the main entity at the picked point
Dim ss1 As AcadSelectionSet
Set ss1 = ThisDrawing.SelectionSets.Add("testSel")
ss1.SelectAtPoint (PickedPoint)
Dim obj2 As Object
Set obj2 = ss1(0)
ss1.Delete
' obj2 is the Main entity at the pick point
Debug.Print obj2.ObjectName
' Ensure the main entity is a rotated dimension
If obj2.ObjectName = "AcDbRotatedDimension" Then
' Get the Owning object, this will be the Block, object is the sub entity selected
Dim obj As Object
Set obj = ThisDrawing.ObjectIdToObject(Object.OwnerID)

Dim myBlk As AcadBlock
Set myBlk = obj
Debug.Print "Selected object owner is a block named " & myBlk.name

Set b = ThisDrawing.Blocks(myBlk.name)

ThisDrawing.ModelSpace.AddCircle b(8).Coordinates, 3
ThisDrawing.ModelSpace.AddCircle b(9).Coordinates, 3
ThisDrawing.ModelSpace.AddCircle b(7).Coordinates, 3
ThisDrawing.Application.Update
Else
MsgBox "Selected entity not a rotated dimension"
End If

End Sub

Regards,
Basha
Message 6 of 6
Anonymous
in reply to: Anonymous

Great work

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report