Issue
I have a folder whose name has single quote mark, e.g. c:\temp\mytest'inventor\. I put a drawing and its referenced part document in the folder. The VBA code gets a curve and creates a dimension. Then overrides the dimension text by FormattedText. The FormattedText wants to use one parameter from the part document. But the result text shows full content of FormattedText.
Sub test()
''Get Drawing
Dim oDoc As DrawingDocument
oDoc = InvApp.ActiveDocument
Dim Path As String
Path = Replace(oDoc.FullFileName, "idw", "ipt")
''Get Part
Dim oPartDoc As PartDocument
oPartDoc = InvApp.Documents.ItemByName(Path)
' assume the first curve is a circle
Dim oView As DrawingView
oView = oDoc.ActiveSheet.DrawingViews.Item(1)
''Get a curve
Dim oOneCurve As DrawingCurve
oOneCurve = oView.DrawingCurves.Item(1)
''Add dimension
Dim Intent As GeometryIntent
Intent = oDoc.ActiveSheet.CreateGeometryIntent(oOneCurve)
Dim oPt As Point2d
oPt = InvApp.TransientGeometry.CreatePoint2d(0, 0)
oPt.X = oView.Center.X + 5
oPt.Y = oView.Center.Y +5
' add a Diameter Dimension
Dim oDim As DiameterGeneralDimension
oDim = oDoc.ActiveSheet.DrawingDimensions.GeneralDimensions.AddDiameter(oPt, Intent)
'' Format dimension and link a part parameter
'assume we have a param named 'myParam'
Dim ParameterName As String
ParameterName = "myParam"
Dim oFormattedText As String
oFormattedText = "<StyleOverride Font='AIGDT'>n</StyleOverride>" & _
"<Parameter ComponentIdentifier='" & _
oPartDoc.FullFileName & _
"' Name='" & _
ParameterName & _
"' Precision='0' ></Parameter>"
oDim.HideValue = True
oDim.Text.FormattedText = oFormattedText
End Sub
Solution
The reason is: the tag (Parameter ComponentIdentifier) of FormattedText needs a full file name string within two single quote marks. While the single quote mark within the full file name truncates the string which causes the FormattedText cannot be parsed correctly.
Sub test()
''Get Drawing
Dim oDoc As DrawingDocument
oDoc = InvApp.ActiveDocument
Dim Path As String
Path = Replace(oDoc.FullFileName, "idw", "ipt")
''Get Part
Dim oPartDoc As PartDocument
oPartDoc = InvApp.Documents.ItemByName(Path)
' assume the first curve is a circle
Dim oView As DrawingView
oView = oDoc.ActiveSheet.DrawingViews.Item(1)
''Get a curve
Dim oOneCurve As DrawingCurve
oOneCurve = oView.DrawingCurves.Item(1)
''Add dimension
Dim Intent As GeometryIntent
Intent = oDoc.ActiveSheet.CreateGeometryIntent(oOneCurve)
Dim oPt As Point2d
oPt = InvApp.TransientGeometry.CreatePoint2d(0, 0)
oPt.X = oView.Center.X + 5
oPt.Y = oView.Center.Y +5
' add a Diameter Dimension
Dim oDim As DiameterGeneralDimension
oDim = oDoc.ActiveSheet.DrawingDimensions.GeneralDimensions.AddDiameter(oPt, Intent)
Dim sPartFileName As String
sPartFileName = oPartDoc.FullFileName
' replace the quotation marks if there are in the file path
If InStr(1, sPartFileName, "'") Then
sPartFileName = Replace(sPartFileName, "'", "'")
End If
'' Format dimension and link a part parameter
'assume we have a param named 'myParam'
Dim ParameterName As String
ParameterName = "myParam"
Dim oFormattedText As String
oFormattedText = "<StyleOverride Font='AIGDT'>n</StyleOverride>" & _
"<Parameter ComponentIdentifier='" & _
sPartFileName & _
"' Name='" & _
ParameterName & _
"' Precision='0' ></Parameter>"
oDim.HideValue = True
oDim.Text.FormattedText = oFormattedText
End Sub
Users should take care of the quotation marks when place them into the FormattedText, they should use the codes instead to represent them. Below is the update code:
o get more info about this, please refer to the Inventor API help for title 'XML Tags for FormattedText', and at the bottom there is a table to list the codes for some certain characters.