Add Text to a Drawing Dimension

Add Text to a Drawing Dimension

Anonymous
Not applicable
665 Views
12 Replies
Message 1 of 13

Add Text to a Drawing Dimension

Anonymous
Not applicable
The following code works in IV9 but not IV11. I get the text box, but the text box entry does not make it to the drawing dimension. What should I change?

Public Sub AddTextToDrawingDimension()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

'User an Inputbox so a user can enter the text he wants behind thedimension
Dim sUserText As String
sUserText = Trim(InputBox("Enter hole Qty. to be placed behind dimension or hit for none.", "Addsuffix to dimension"))

' Find all selected occurrences and add them to an ObjectCollection.
Dim oDrawingDims() As DrawingDimension
Dim a As Integer
a = 0
Dim i As Long
For i = 1 To oDrawDoc.SelectSet.Count
If Not oDrawDoc.SelectSet.Item(i) Is Nothing Then
If TypeOf oDrawDoc.SelectSet.Item(i) Is DrawingDimension Then
'Only add Dimensions to the ObjectCollection
a = a + 1
ReDim Preserve oDrawingDims(0 To a)
Set oDrawingDims(a) = oDrawDoc.SelectSet.Item(i)
End If
End If
Next
Dim n As Integer
For n = 1 To a
'MsgBox " text is " & sUserText
If sUserText = "" Then
oDrawingDims(n).Text.FormattedText = "<<>>"
Else
oDrawingDims(n).Text.FormattedText = "(" & sUserText & ")"
End If
' for IV 9, replace with << >>
Next
End Sub


Thanks,

Jon
0 Likes
666 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable
You need to add the prefix. I've modified the code below.

Public Sub AddTextToDrawingDimension()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

'User an Inputbox so a user can enter the text he wants behind
thedimension
Dim sUserText As String
sUserText = Trim(InputBox("Enter hole Qty. to be placed behind dimension
or hit for none.", "Addsuffix to dimension"))

' Find all selected occurrences and add them to an ObjectCollection.
Dim oDrawingDims() As DrawingDimension
Dim a As Integer
a = 0
Dim i As Long
For i = 1 To oDrawDoc.SelectSet.Count
If Not oDrawDoc.SelectSet.Item(i) Is Nothing Then
If TypeOf oDrawDoc.SelectSet.Item(i) Is DrawingDimension Then
'Only add Dimensions to the ObjectCollection
a = a + 1
ReDim Preserve oDrawingDims(0 To a)
Set oDrawingDims(a) = oDrawDoc.SelectSet.Item(i)
End If
End If
Next
Dim n As Integer
For n = 1 To a
'MsgBox " text is " & sUserText
If sUserText = "" Then
oDrawingDims(n).Text.FormattedText = ""
Else
oDrawingDims(n).Text.FormattedText = "" & "(" &
sUserText & ")"
End If
' for IV 9, replace with << >>
Next
End Sub


Sanjay-

wrote in message news:5623934@discussion.autodesk.com...
The following code works in IV9 but not IV11. I get the text box, but the
text box entry does not make it to the drawing dimension. What should I
change?

Public Sub AddTextToDrawingDimension()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

'User an Inputbox so a user can enter the text he wants behind
thedimension
Dim sUserText As String
sUserText = Trim(InputBox("Enter hole Qty. to be placed behind dimension
or hit for none.", "Addsuffix to dimension"))

' Find all selected occurrences and add them to an ObjectCollection.
Dim oDrawingDims() As DrawingDimension
Dim a As Integer
a = 0
Dim i As Long
For i = 1 To oDrawDoc.SelectSet.Count
If Not oDrawDoc.SelectSet.Item(i) Is Nothing Then
If TypeOf oDrawDoc.SelectSet.Item(i) Is DrawingDimension Then
'Only add Dimensions to the ObjectCollection
a = a + 1
ReDim Preserve oDrawingDims(0 To a)
Set oDrawingDims(a) = oDrawDoc.SelectSet.Item(i)
End If
End If
Next
Dim n As Integer
For n = 1 To a
'MsgBox " text is " & sUserText
If sUserText = "" Then
oDrawingDims(n).Text.FormattedText = "<<>>"
Else
oDrawingDims(n).Text.FormattedText = "(" & sUserText & ")"
End If
' for IV 9, replace with << >>
Next
End Sub


Thanks,

Jon
0 Likes
Message 3 of 13

Anonymous
Not applicable
Sanjay,

I copied your code and still I get the same behavior, which is no text. Though I do get the input box. Have you tried your code?

Jon
0 Likes
Message 4 of 13

Anonymous
Not applicable
I've tried this code and it works fine.
Make sure you hit ok after enter text in the input box
CVA

--
www.CVAengineering.com

IV11 Pro. sp2
Window XP Pro sp2
Pentium 3.2 Ghz, 3.0 GB of RAM
NVIDIA FX 3400 84.26
SpacePilot V 1.1.2
wrote in message news:5626453@discussion.autodesk.com...
Sanjay,

I copied your code and still I get the same behavior, which is no text.
Though I do get the input box. Have you tried your code?

Jon
0 Likes
Message 5 of 13

Anonymous
Not applicable
I did upgrade to SP2 thinking that might be helpful. It was not. When I run the macro I get no errors, but also nothing happens to the text. It's a bit maddening and I'm not sure what to do next.

Jon
0 Likes
Message 6 of 13

Anonymous
Not applicable
I have exactly the same macro at work with all sorts loaded into it,
diameter symbols, "Typ", well same theroy anyway.

I believe they may have changed the dimension objects or split them, where
you used to use, or still are trying to use the "DrawingDimension" object, I
believe it may now be something like "LinearDimension" object you need to
look for, the API changed.

HTH

Scott
0 Likes
Message 7 of 13

Anonymous
Not applicable
Scott,

Are you using IV 9 or IV 11 at work? If IV 11, is it possible to check your code? I'm still working at it. Thanks.

Jon
0 Likes
Message 8 of 13

Anonymous
Not applicable
Hello Jon,
We have recently moved from IV9 to IV 11 Sp2. I will double check my code
tomorrow and let you know precisely the Dimension object I now attach my
text to.

Scott
wrote in message news:5629743@discussion.autodesk.com...
Scott,

Are you using IV 9 or IV 11 at work? If IV 11, is it possible to check your
code? I'm still working at it. Thanks.

Jon
0 Likes
Message 9 of 13

Anonymous
Not applicable
Hello Jon,
I go about this differently to you, I make a selection set of the dimensions
I wish to add text to, then pass the selection set to be processed, as of
consequence I use a Generic Object type to to iterate through the selection
set in a for Each loop, something like For Each GenericObject in
Document.SelectionSet and attach my text there due to all the new
dimension(LinearDimension, RadiusDimension and DiameterDimension) objects
pretty much supporting the same properties, so I'll use the
GenericObject.Text property Object to change the dimension text.
I tried today to post from work, the website told me I was logged in when I
arrived, however when I wanted to attach a file, I was asked to log in, this
I did, I was then asked to log in again, this I did, I was then asked to log
in again, again I logged in, then I was again asked to log in, I got bored,
when I returned to my message it was a revision earlier and too many edits
had been lost.
I'll bring everything home tomorrow and post from home.

Scott
0 Likes
Message 10 of 13

Anonymous
Not applicable
Scott,

Thanks so much. I have a non-work email that I will publish if this doesn't work at your home, which I'm sure the user group would appreciate.

Jon
0 Likes
Message 11 of 13

Anonymous
Not applicable
Hello Jon,
Yay, I have everything. 🙂

Right I don't know if this will be hard to follow or not...
I start by making a selection set on my drawings of all the dimensions I
wish to add text to, then I invoke this routine,

Public Sub AddTypText()
If Not CheckSelectSet(ThisApplication.ActiveDocument) Then
MsgBox "No selections have been made.", vbOKOnly, "Nothing
Selected"
Exit Sub
End If

AddText ThisApplication.ActiveDocument.SelectSet, vbCrLf & "Typ",
False
End Sub

This sub will add "Typ" to all the dimensions I have selected on my drawing
sheet, however before this will happen I make sure something is selected by
using the "CheckSelectSet" function here...

Private Function CheckSelectSet(pvarDoc As Document) As Boolean
If pvarDoc.SelectSet.Count > 0 Then CheckSelectSet = True
End Function

Now I know I have made selections, we can continue 🙂 This is the nitty
gritty bit...

Public Sub AddText(pvarSelectSet As SelectSet, pvarText As String,
pvarBefore As Boolean)
Dim varObject As Object

For Each varObject In pvarSelectSet
If pvarBefore Then
varObject.Text.FormattedText = PreText(pvarText,
varObject.Text.FormattedText)
Else: varObject.Text.FormattedText = PostText(pvarText,
varObject.Text.FormattedText)
End If

Next varObject
End Sub

As I've mentioned before, due to the new dimensions sharing similar object
properties, I iterate through the selection set using a Generic object
variable. The dimension text I want to modify is obtained from this portion
here, "varObject.Text.FormattedText", this is where the existing dimension
text is held as far as I'm concerned, as you will see from the two functions
below, they use "varObject.Text.FormattedText" in order to obtain the
correct value(assumption here, don't quote me) of the dimension and then add
the selected text to the dimension text, depending on which function has
been called will define where the text is placed with respect to the
dimension text.

From "Public Sub AddTypText()" and in particular this line, "AddText
ThisApplication.ActiveDocument.SelectSet, vbCrLf & "Typ", False", you can
see I wish "Typ" to be added to all the dimensions I have selected at the
end of the dimension text, "False" stipulates this at the end of the line

The two Functions called from "Public Sub AddText(pvarSelectSet As
SelectSet, pvarText As String, pvarBefore As Boolean)" create the complete
string of dimension text to be assigned to the dimension...

Private Function PreText(pvarText As String, pvarDimText As String) As
String
PreText = pvarText & pvarDimText
End Function

and

Private Function PostText(pvarText As String, pvarDimText As String) As
String
PostText = pvarDimText & pvarText
End Function

One of these functions will be called depending on where you would like your
text to be placed, both use the existing text of the dimension and either
add the new text to the beginning of the dimension text or at the end.

A routine that has no value whatsoever when coding, however when in a tight
corner can really let you know what you're dealing with...

Public Sub CheckSelections()
Dim varSelect As Inventor.SelectSet
Set varSelect = ThisApplication.ActiveDocument.SelectSet
End Sub

This routine works on this basis, I have one variable declared "varSelect",
I will create a Selection in the current document and then Step(F8) through
this routine, this will execute one line of code at a time. Have a look at
the picture. I stepped into this routine having made a selection of two
dimensions on my drawing document, using the "Locals Window"(View -> Locals
Window) I can see exactly what I have selected, what it is defined as being
and all the properties it supports. I believe it was this routine that got
me out of the predicament you're in now when I bumped into the same problem.

Hope this helps

Scott
0 Likes
Message 12 of 13

Anonymous
Not applicable
Picture
0 Likes
Message 13 of 13

Anonymous
Not applicable
Scott,

Very nice! 🙂 This will give me some things to work with - options. I'll let you know how it turns out.

Jon
0 Likes