Getting Started - iterate through Dimensions & show style.

Getting Started - iterate through Dimensions & show style.

andrew_canfield
Collaborator Collaborator
677 Views
4 Replies
Message 1 of 5

Getting Started - iterate through Dimensions & show style.

andrew_canfield
Collaborator
Collaborator

Hello 

I'd like to build on my basic iLogic know how - the grand plan is to swap out dimension styles - I've seen some posts but I'm not familiar with the code. Generally iteration is a mystery.

I'd like to start off simple. A message box popping up for each dimension found.

What code will display the style used by a dimension?

 

For each dimension on a sheet display the dimension style.

next

 

Then follow with:

if  style = xyz then replace with abc

 

Hopefully i'll be able to build from there.

 

Regards

 

Andrew

 

 

 

 

 

0 Likes
Accepted solutions (1)
678 Views
4 Replies
Replies (4)
Message 2 of 5

DRoam
Mentor
Mentor
Accepted solution

The API reference manual is extremely handy for learning how to do new things in iLogic. Here's a link to the main page: Inventor API User's Manual. I'd recommend bookmarking it. Once you get to that page, expand the "Inventor API Reference Manual" node in the tree on the left, and then expand the "Objects" node. You can then use Ctrl+F to search for terms (like "Dimension" or "Style") to see what objects there are, how to get to them (by following the "Parent Object" links at the top, or "Accessed From" links near the bottom), and what methods/properties are available for them.

 

For example:

  • Use Ctrl+F and search for "Style". After hitting enter a few few times, you'll find "DimensionStyle". Click this one.
  • From here you can scroll down through the properties, and you'll find one called "Name", with the description "Gets/Sets the name of the Style." So now we know how to get or set a Dimension Style once we've accessed it.
  • Now we need to know how to access a Dimension Style. Scroll down to the "Accessed From" section. Scanning through, you'll see there's one called "GeneralDimension.Style". That sounds like it would be a standard dimension in a drawing. So now we know how to access a dimension's style once we've accessed the dimension.
  • Now we need to know how to access a dimension. So click the link for "GeneralDimension.Style". This takes us to a page for the "GeneralDimension.Style" property. We want the parent of this, the "GeneralDimension". So click that link at the top. Then scroll down to the "Accessed From" section. You'll see there's one called "GeneralDimensions.Item". So a GeneralDimension is accessed from a GeneralDimensions enumerator. So now we know how to access a GeneralDimension, or loop through several of them, once we've accessed a GeneralDimensions enumerator.
  • Now we need to know how to access a GeneralDimensions enumerator. So click that link, then click the link for the parent object "GeneralDimensions". Scroll down to "Accessed From"
  • You get the idea...

 

If you keep following this path, eventually you'll find that this is the access path you need:

 

DrawingDocument.Sheets.Item().DrawingDimensions.GeneralDimensions.Item().Style.Name

DrawingDocument can be accessed either using "ThisDoc.Document" or "ThisApplication.ActiveDocument", depending on how your rule will be used.

 

Anywhere that you see "Item()" is a place where you'll need to iterate through the collection.

 

So, all that said, the code to display a messagebox for each dimension's style would look something like this:

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisDoc.Document

For Each oSheet As Sheet In oDrawDoc.Sheets
	For Each oGenDim As GeneralDimension In oSheet.DrawingDimensions.GeneralDimensions
		oStyleName = oGenDim.Style.Name
		MessageBox.Show(oStyleName)
	Next 'oGenDim on sheet
Next 'oSheet in drawing

And of course, that's just the beginning. You can use the lists of Methods and Properties under each Object type to see all of the things you can do with each object along the path.

 

That should get you started. I could have just given you the code, but you know the saying about teaching a man to fish 😉 hope this helps!

Message 3 of 5

DRoam
Mentor
Mentor

A couple more general tips, since you mentioned iteration is a little confusing (and it can be sometimes).

 

First, you'll notice that sometimes I put an "o" in front of a name like "oDrawDoc" and "oSheet", and other times I didn't. The times I didn't, like "Sheet" and "GeneralDimension", is because those are object classes that the software uses (that's why they're listed under the Objects node in the API reference manual).

 

oSheet and oGenDim, on the other hand, are instances of those object classes. I could have named them anything (except for one of the Inventor or VB classes/functions that are off-limits), but it's handy to name it something similar to the Class that it represents. So I just prefixed the object's class name with an "o", which is pretty standard practice (I'm guessing it stands for "object").

 

Whenever you want to work with an instance of some class of object (like a sheet, dimension, etc.), you first have to declare a new object as an instance of that class, and then initialize the object with some value.

 

So in the rule, this line:

 

Dim oDrawDoc as DrawingDocument

 

 

means "cast (or declare) oDrawDoc as a new object instance of the DrawingDocument class".

 

The next line:

 

oDrawDoc = ThisDoc.Document

 

 

means "initialize the oDrawDoc object by assigning ThisDoc.Document as its value". Now we can work with the current drawing document by using the oDrawDoc object.

 

Regarding the For loop specifically, you can see that, in contrast to how I explicitly declared oDrawDoc, for oSheet I simply said "For Each oSheet as Sheet..."

 

This is a nice, streamlined way to declare an object that you'll ONLY be using in the For loop. Once the last iteration of the "sheets" for loop is finished, or the loop is exited, the oSheet object will be wiped from memory, and you won't be able to use it anywhere after the for loop (you'll get an error if you try to). This is handy for some situations. However, if you DID need to use a sheet that your For loop found, you would need to declare the oSheet object OUTSIDE of the For loop, like this:

 

 

Dim oSheet As Sheet

For Each oSheet In oDrawDoc.Sheets
     ...

 

Hope all of that is helpful. I'm not a programming expert by any means (so some of what I said may be technically incorrect), but I've found that learning about classes, objects, methods, properties, etc. has been especially helpful to me in expanding my practical iLogic abilities. Hope you find the same 🙂

 

0 Likes
Message 4 of 5

andrew_canfield
Collaborator
Collaborator

Many thanks - 

I've already started some random changes:

adding two lines just to test - returns 'true' - all good. - the code works 🙂

SyntaxEditor Code Snippet

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisDoc.Document

For Each oSheet As Sheet In oDrawDoc.Sheets
    For Each oGenDim As GeneralDimension In oSheet.DrawingDimensions.GeneralDimensions
    
        oStyleName = oGenDim.Style.Name        
        MessageBox.Show(oStyleName)
        
        oStyleac = oGenDim.style.AngularArrowsInside()        
        MessageBox.Show(oStyleac)    
        
        
    Next 'oGenDim on sheet
Next 'oSheet in drawing

changing  one line from:

oStyleac = oGenDim.style.AngularArrowsInside()    

 to the line below, not so good 😞

oStyleac = oGenDim.style.Color()

 I had half an idea that this might show the color of the text but, 'string not valid' is returned.

Thinking the message box could show the dimension value in a later step.

Would this be the murky world of classes? Where values aren't the same, like text isn't a number etc..

Message 5 of 5

DRoam
Mentor
Mentor

If you take a look at the DimensionStyle Object page, and click on the "Color" property, you'll see it actually returns a Color object. Essentially, it's made up of an RGB value and an opacity value. And unfortunately, the Color class doesn't have a "Name" property, because there isn't a name associated with every possible color on the spectrum. So you won't be able to just show "Blue" or "Green" or "Purple" in your messagebox. However, you could easily show the RGB values like this:

 

 

Dim oColor As Color
oColor = oGenDim.Style.Color

oStyleac = oColor.Red & "," & oColor.Green & "," & oColor.Blue
MessageBox.Show(oStyleac)

 

You could also check the RGB values against those for some color that you're looking for.

 

 

As for showing dimension value, if you look at the GeneralDimension object page, it has a "Text" property. If you click this, you'll see it actually returns a DimensionText object rather than just the dimension's text. Within the DimensionText object is another Text property that will finally return the dimension's text value (you may want to take a look at the description notes for the Text property if you plan on ever trying to modify the dimension's text).

 

So to get your dimension's text value, you would say:

 

oStyleac = oGenDim.Text.Text
MessageBox.Show(oStyleac)
0 Likes