Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding Text to Inventor Dimension with iLogic or VBA

22 REPLIES 22
SOLVED
Reply
Message 1 of 23
ccoomes
7396 Views, 22 Replies

Adding Text to Inventor Dimension with iLogic or VBA

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..

22 REPLIES 22
Message 2 of 23
Vladimir.Ananyev
in reply to: ccoomes

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

Message 3 of 23
GosponZ
in reply to: Vladimir.Ananyev

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

Message 4 of 23
Vladimir.Ananyev
in reply to: GosponZ

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

Message 5 of 23
GosponZ
in reply to: Vladimir.Ananyev

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

 

Message 6 of 23
Vladimir.Ananyev
in reply to: GosponZ

No problem.  Try this:

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

 Dim.PNG

cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 7 of 23
GosponZ
in reply to: Vladimir.Ananyev

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

Message 8 of 23
Vladimir.Ananyev
in reply to: GosponZ

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

Message 9 of 23
GosponZ
in reply to: Vladimir.Ananyev

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.

Message 10 of 23
Vladimir.Ananyev
in reply to: GosponZ

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

Message 11 of 23
GosponZ
in reply to: Vladimir.Ananyev

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

 

Message 12 of 23
Vladimir.Ananyev
in reply to: GosponZ

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

Message 13 of 23
GosponZ
in reply to: Vladimir.Ananyev

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

 

Message 14 of 23
Vladimir.Ananyev
in reply to: GosponZ

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

Message 15 of 23
GosponZ
in reply to: Vladimir.Ananyev

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?
Message 16 of 23
ccoomes
in reply to: GosponZ

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

 

Message 17 of 23
GosponZ
in reply to: ccoomes

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

Message 18 of 23

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

Message 19 of 23
leevercarter
in reply to: GosponZ

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,

 

Message 20 of 23

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.

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

Post to forums  

Autodesk Design & Make Report