Adding Text to Inventor Dimension with iLogic or VBA

ccoomes
Advocate
Advocate

Adding Text to Inventor Dimension with iLogic or VBA

ccoomes
Advocate
Advocate

Does anyone know how to add pre-defined text to a Selected Dimension with either iLogic or VBA?

 

I have to create lots of drawing with a lot of dimensions with the same wording.

 

The Layout is as follows:

 

<DIM> Maximum

Product Width

 

What I would like to do is select the Dimension, and then click and button on a form (or VBA USerform) that adds pre-defined text.

 

Has anyone been able to do this as it will make my life a lot simpler..

Reply
Accepted solutions (1)
8,414 Views
23 Replies
Replies (23)

Vladimir.Ananyev
Alumni
Alumni

You may change DimensionText.FormattedText property in the selected dimension.

Try the following VBA sample. It adds "ZZZ" string to the selected dimension text.

 

Sub DIM_ChangeText()
  Dim oDoc As DrawingDocument
  Set oDoc = ThisApplication.ActiveDocument
  Dim oSSet As SelectSet
  Set oSSet = oDoc.SelectSet
  'reference to the selected dimension
  Dim oDim As LinearGeneralDimension
  Set oDim = oSSet.Item(1) 
  'refrence to the DimensionText object
  Dim oDimensionText As DimensionText
  Set oDimensionText = oDim.Text
  'print text content
  Debug.Print oDimensionText.Text
  Debug.Print oDimensionText.FormattedText
  'change text
  oDimensionText.FormattedText = "<DimensionValue/> ZZZ"
  Beep
End Sub

 cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

GosponZ
Collaborator
Collaborator

Vladimir,

i removed set in code to make ilogic and code working just perfect. Here is questions:

Code doesn't work with radius dim, and would be nice if text could be in next row. Can this be done?

Also multi value of text to choose which text to pick.

Thank you for your help

0 Likes

Vladimir.Ananyev
Alumni
Alumni

You may work with radius dims and with linear dims if declare object variable oDim as base class GeneralDimension. 

If you need staked text you may use XML tag <Stack> (see Inventor API Help for “XML Tags for FormattedText“ for more information).

Here is updated sample:

 

Sub DIM_ChangeText_2()
  Dim oDoc As DrawingDocument
  Set oDoc = ThisApplication.ActiveDocument
  Dim oSSet As SelectSet
  Set oSSet = oDoc.SelectSet
  'reference to the selected dimension
  Dim oDim As GeneralDimension
  Set oDim = oSSet.Item(1)
 
  'reference to the DimensionText object
  Dim oDimensionText As DimensionText
  Set oDimensionText = oDim.Text
 
  'change dimension text
  oDimensionText.FormattedText = "<DimensionValue/> <Stack>AAA/BBB</Stack>"
  Beep
End Sub

 Hope this helps.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

GosponZ
Collaborator
Collaborator

It is working but i prefer text to be below dimension.

 

0 Likes

Vladimir.Ananyev
Alumni
Alumni

No problem.  Try this:

oDimensionText.FormattedText = "<DimensionValue/><br/>AAA<br/>BBB<br/>CCC"

 Dim.PNG

cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

GosponZ
Collaborator
Collaborator

Vladimir,

as always you made this perfect.

I do have final question.Can we make this so text to be pulled either from excel or from multi value parameter.There is a lot text in drawing s as TYP., O.C. etc etc

 

Thank You

0 Likes

Vladimir.Ananyev
Alumni
Alumni

Let’s suppose we have created the following iLogic rule in the drawing document.  (Of course, you may change dimension text from the rule in the model document. It’s up to you.)

This rule:

1) finds the reference to the model file that was used to create the drawing.

2) read the value of the text parameter in the model document. This parameter could be multivalue one.  Here parameter value is saved in the string variable Value.  

3) finds the reference to the dimension object.  (Here I used the second dimension in the active sheet - for simplicity).

4) changes dimension text adding the string Value below the current dimension text.

'get model document (part or assembly) from the parent DrawingDocument
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
'save text parameter "MyText" value in the string variable Value
Dim Value As String = Parameter(modelName, "MyText")
'Note:  this string Value could be read from any source (e.g., excel sheet).

Dim oDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Sheet =  ActiveSheet.Sheet
'reference to some dimension
Dim oDim As GeneralDimension = oSheet.DrawingDimensions.Item(2)
'refrence to the DimensionText object
Dim oDimensionText As DimensionText = oDim.Text

'create text that should be placed below the dimension
Dim St As String = "<br/>" & Value

'change dimension text
oDimensionText.FormattedText = "<DimensionValue/>" & St

 Now if you run this rule you will see the model parameter value in the second line of dimension text.

Smiley Happy


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes

GosponZ
Collaborator
Collaborator

I do have 3 views in my sheet. Code is working, but why is working only on one dimension. If i need on secon dim. to add text it happend nothing. No error but no changes.

0 Likes

Vladimir.Ananyev
Alumni
Alumni
Accepted solution

Select at least one dimension and run the following rule.

It filters GeneralDimension objects (ignoring any other selected entities)

and then modifies dimension text in all selected dimensions as was described earlier.

Button on iLogic form should be useful to initiate rule execution.

 

'get model document (part or assembly) from the parent DrawingDocument
modelName = IO.Path.GetFileName(ThisDrawing.ModelDocument.FullFileName)
'save text parameter "MyText" value in the string variable Value
Dim Value As String = Parameter(modelName, "MyText")
'Note:  this string Value could be read from any source (e.g., excel sheet).

Dim oDoc As DrawingDocument = ThisDrawing.Document
Dim oSSet As SelectSet = oDoc.SelectSet

If oSSet.Count = 0 Then
    MessageBox.Show("Select at least one dimension please", "DIM", _
        MessageBoxButtons.OK,MessageBoxIcon.Information)
Else
    For Each obj As Object In oSSet
        'filter dimensions, ignore other selected entities
        If TypeOf obj Is GeneralDimension Then
            'reference to the selected dimension
            Dim oDim As GeneralDimension = obj
            'refrence to the DimensionText object
            Dim oDimensionText As DimensionText = oDim.Text        
            'change dimension text
            oDimensionText.FormattedText = "<DimensionValue/>" & "<br/>" & Value
        End If
    Next
End If
Beep

 Cheers,

 


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

GosponZ
Collaborator
Collaborator

Yes, it is working. Time saver. Thanks a lot Vladimir.

Before, ending this case one more question. If parameter MyText is made as Multivalue, what in code need to be changed

 

0 Likes

Vladimir.Ananyev
Alumni
Alumni

Do not worry - In my own tests parameter MyText was created as Multivalue.  

So nothing should be changed.  Enjoy! Smiley Happy

 

Do not forget to mark this iLogic rule as a solution please.

 


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes

GosponZ
Collaborator
Collaborator

I can't see Mark as solution button to accept.

 

0 Likes

Vladimir.Ananyev
Alumni
Alumni

Sign in and you will see Accept as Solution button in the right bottom corner of every post.

Another way - via Options drop-down list...


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes

GosponZ
Collaborator
Collaborator
I'm signed in. I know, I'm always accepting. But there is not such option. I do have reply - quick reply button and in options there is not that option too. Is this because someone else start with this post?
0 Likes

ccoomes
Advocate
Advocate

I have not been able to try the code suggested, but I will this afternoon.

 

Below is what I got to work.  I know it checks to see if the Dimensions value is hidden, but I could not figure out how to remve this bit and get it to work...

 

It does however allow you to select more than 1 Dimension and I use the chr(10) to get the text onto a second line.

 

This is based in VBA and not iLogic:

 

'Set a reference to the select set of the active document.
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oDrawingDims() As DrawingDimension

Dim oItemsCount As String
oItemsCount = oDoc.SelectSet.Count

Dim oDimText As String
oDimText = " Maximum" & Chr(10) & "Product Width"

If Not oItemsCount = 1 Then
Call MessageBoxes.NotASingleDimension
Exit Sub
Else
Dim a As Long
a = 0
Dim i As Long
For i = 1 To oDoc.SelectSet.Count
If Not oDoc.SelectSet.Item(i) Is Nothing Then
If TypeOf oDoc.SelectSet.Item(i) Is DrawingDimension Then
a = a + 1
ReDim Preserve oDrawingDims(0 To a)
Set oDrawingDims(a) = oDoc.SelectSet.Item(i)
Else
Call MessageBoxes.NotASingleDimension
Exit Sub
End If
End If
Next
Dim b As Long
For b = 1 To a
If oDrawingDims(b).HideValue = False Then
oDrawingDims(b).Text.FormattedText = oDimText
Else
End If
Next
End If

 

0 Likes

GosponZ
Collaborator
Collaborator

Changed code just a hair. Now parameter is in dwg and parameter is multi value parameter.

I made form with rule and parameters so i can choose which text to use with dimension.


dwgParam=Parameter(modelName, "MyText")

'save text parameter "MyText" value in the string variable Value
DimValueAsString=Parameter(DocName, "MyText")

DimoDocAsDrawingDocument=ThisDrawing.Document
DimoSSetAsSelectSet=oDoc.SelectSet

IfoSSet.Count=0Then
MessageBox.Show("Select at least one dimension please", "DIM", _
MessageBoxButtons.OK,MessageBoxIcon.Information)
Else
ForEachobjAsObjectInoSSet
'filter dimensions, ignore other selected entities
IfTypeOfobjIsGeneralDimensionThen
'reference to the selected dimension
DimoDimAsGeneralDimension=obj
'refrence to the DimensionText object
DimoDimensionTextAsDimensionText=oDim.Text
'change dimension text
oDimensionText.FormattedText="<DimensionValue/>"&"<br/>"&Value
EndIf
Next
EndIf
Beep

Anonymous
Not applicable

Vladimir

 

I have used your code. Many thanks. I have a question which is hopefully a very small modification of this code.

 

Is it possible to amend this code so that instead of inserting the current parameter value into the dimension, it will insert a 'reference' to the parameter, so that if the parameter value changes, the value shown in the dimension will update automatically.

 

If I insert a parameter into the dimension text using the dimension text formatting dialog box, the value will change when the parameter changes. Could your code be amended to achieve the same result?

 

Many thanks

0 Likes

Anonymous
Not applicable

MisterZS

 

This method worked very well for me, I have been able to edit it to my needs and create a form which makes it easy to add text from a multi value paramter stored in the idw. This also has made it easy to save to our template so all new drawings we make have this function with the form and rule in the idw.

 

However I have been unable to change the rule to be able to add the text before the dimension value for only certain parameter selections. For example a diameter symbol needs to come before. I would like in the form when I select the diamter symbol from the multi value parameter, the rule to then place that symbol before the text. If that could be done I'd be very grateful.

 

Thank you for your time,

 

0 Likes

Anonymous
Not applicable

hello mr.Vladimir Ananyev

I have two part documents in an assembly and i have model parameters in assembly. i want link the model parameter which is in the assembly document to the dimension text as we do manually by editing dimension text.

And how did you get this one...  " value = Parameter(ModelName,"mytext")  "

i tried writing this line in my project, but after entering parameter its not allowing me to keep (...), so can i know how you gave model name and parameter name inside brackets ?

If possible can you give some good code how to link parameters to dimension text please.

0 Likes