Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA for Inventor - Get Object properties

Status
Not open for further replies.

ACEDeSmedt

Technical User
Oct 25, 2008
3
NL
I'm writing a macro that replace text somewhere on a drawing, in a iproperty or in a titleblock.

The tings I want is a macro that looks for the text in all the properties of ThisApplication en report back the location.

The next step is a other macro that replace the text on the location, given from the previous macro.

Thanks in advance for any help

ing. Klaas De Smedt
 




For what application?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 




Have you tried a forum that is for Inventor?

Do you understand the Inventor Object Model?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
In a new drawing, create a dtext with "Your name is Klaas". Create an mtext with "My name is GVF". The code below will toggle the text between the two text boxes and report the insert point for both. At the end it moves the Mtext box to 0,0,1. It assumes WCS. If you are using a defined UCS, you will need to jump through hoops but it can be done.

Code:
Option Compare Text
Sub GetText()
'On Error Resume Next
Dim MyBlock, Counter, InsertPt
For Counter = 0 To ThisDrawing.ModelSpace.Count - 1
If ThisDrawing.ModelSpace.Item(Counter).ObjectName = "AcDbText" Then
    Set MyBlock = ThisDrawing.ModelSpace.Item(Counter)
        If MyBlock.TextString = "My name is GVF" Or MyBlock.TextString = "Your name is Klaas" Then
            If MyBlock.TextString = "My name is GVF" Then
                MyBlock.TextString = "Your name is Klaas"
            ElseIf MyBlock.TextString = "Your name is Klaas" Then
                MyBlock.TextString = "My name is GVF"
            End If
            InsertPt = MyBlock.InsertionPoint
            MsgBox "X= " & InsertPt(0) & vbCr & "Y= " & InsertPt(1) & vbCr & "Z= " & InsertPt(2)
        End If
ElseIf ThisDrawing.ModelSpace.Item(Counter).ObjectName = "AcDbMText" Then
    Set MyBlock = ThisDrawing.ModelSpace.Item(Counter)
        If MyBlock.TextString = "My name is GVF" Or MyBlock.TextString = "Your name is Klaas" Then
            If MyBlock.TextString = "My name is GVF" Then
                MyBlock.TextString = "Your name is Klaas"
            ElseIf MyBlock.TextString = "Your name is Klaas" Then
                MyBlock.TextString = "My name is GVF"
            End If
            InsertPt = MyBlock.InsertionPoint
            MsgBox "X= " & InsertPt(0) & vbCr & "Y= " & InsertPt(1) & vbCr & "Z= " & InsertPt(2)
            InsertPt(0) = 0
            InsertPt(1) = 0
            InsertPt(2) = 1
            MyBlock.InsertionPoint = InsertPt
        End If
End If
Next Counter
End Sub
 
Nice example GVF.

But now comes the tricky part. What if the search string is in a iproperty or in a custom property or in the title block ??

Do I have to search every property(Here Modelspace.Item(count)) one by one or is there a way I can search them all at once ?
 
Klaas,
You can use SelectOnScreen to limit the selection set. You can then step through the items in this limited selection set, stop on the items you want and alter their properties.
If you aren't sure what you want, then you will be stuck with ThisDrawing.ModelSpace.Count that comprises the global selection set.
If the titleblock has attributes you can check the tagstring of each attribute and then change the text string of the proper attribute.
Not sure what an iproperty is but a regular property (textstring, color, layer etc.) should be a property of an object that is contained in modelspace.count or a selectionset.
At any rate here is a snippet to change attributes in a block called "TitleBlock"...

Code:
For Counter = 0 To ThisDrawing.ModelSpace.Count - 1
If ThisDrawing.ModelSpace.Item(Counter).ObjectName = "AcDbBlockReference" Then
    Set MyBlock = ThisDrawing.ModelSpace.Item(Counter)
            MyAtt = MyBlock.GetAttributes
                If UBound(MyAtt) = -1 Then GoTo NextI 'in case the block doesn't have any attributes
            For I = LBound(MyAtt) To UBound(MyAtt)
                If MyAtt(I).TagString = "Name" Then
                    MyAtt(I).TextString = "Your Name"
                ElseIf MyAtt(I).TagString = "Project" Then
                    MyAtt(I).TextString = "12345"
                ElseIf MyAtt(I).TagString = "Title" Then
                    MyAtt(I).TextString = "Big Ugly Bracket"
                End If
NextI:
            Next I
End If
Next Counter

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top