Quick Ilogic question

Quick Ilogic question

Anonymous
Not applicable
654 Views
10 Replies
Message 1 of 11

Quick Ilogic question

Anonymous
Not applicable

Hi i'm not very well versed in ilogic so i found a script on the forum to do exactly what i'd like to do. The only issue is i need it to check both that the feature is a hole and that it is a certain diameter. I'd like to change the line below so I can and diameter condition.

 

If TypeOf oFeature Is HoleFeature Then

I'd like to add an additional condition here to only preform the next statement if the hole is let's say .125 diameter.  I'm assuming this isn't very difficult but I'm very new to this. Any help would be greatly appreciated. Thanks!

0 Likes
655 Views
10 Replies
Replies (10)
Message 2 of 11

jholland3XDLM
Advocate
Advocate

you can use the word And

for example 

 

If TypeOf oFeature Is HoleFeature And holeDiameter = 0.125 Then

 

 

I am assuming a  parameter for the diameter of the hole is named holeDiameter, you could use d120 or whatever the parameter name is. If it is a valid parameter name it should turn blue in the iLogic browser.

0 Likes
Message 3 of 11

AdamAG99T
Advocate
Advocate

Once you determine that the oFeature is a hole then you should be able to get the value from oFeature.HoleDiameter. Putting them both in the same If statement would work but could throw some errors on non-hole features, so this is likely the simplest method.

 

If TypeOf oFeature Is HoleFeature Then
If oFeature.HoleDiameter=.125 Then

'Code here

End If
End If

 

0 Likes
Message 4 of 11

Anonymous
Not applicable

 

Thanks for your help

I added the code as you said but i'm getting an error. It says overload resolution failed because no public '=' can be called with these arguments. 'Public Shared Operator =(left as double, right as foublte) as Boolean': Argument matching parameter 'left; cannot convert from '_ComObject' to 'Double'.

Any help would be greatly appreciated!

0 Likes
Message 5 of 11

AdamAG99T
Advocate
Advocate

My apologies, I forgot you need to add .Value to the end of the HoleDiameter. Also of note is the fact that inventor uses cm for all of it's internal value storage so make sure you convert your measurements appropriately.

If TypeOf oFeature Is HoleFeature Then
If oFeature.HoleDiameter.Value=.3175 Then '.3175cm=.125in

'Code here

End If
End If

0 Likes
Message 6 of 11

jholland3XDLM
Advocate
Advocate

Separating into two If statements is a good idea.

 

I did not know that a variable could be associated to a Feature like Adam suggested and I would like more information on that. In the meantime, I would just remove the "oFeature." part of the second if statement. It is already only going to check the holeDiameter when oFeature is a HoleFeature. 

 

So as long as that parameter is present and set to something every time that oFeature is a HoleFeature then you are ok. If there are instances where holeDiameter is not present or is set to something other than a number then you will need to add additional code to figure that out.

 

0 Likes
Message 7 of 11

AdamAG99T
Advocate
Advocate

I'm assuming this code runs through every feature in the part file, so the oFeature is needed in the 2nd if statement to point the HoleDiameter.Value to the specific HoleFeature and it's associated information. oFeature.HoleDiameter specifically pulls the associated parameter controlling the diameter of the hole feature that oFeature is referring to at that present moment in the code, regardless of what that parameter is actually named.  

Since you mentioned wanting to know more information on this, I mainly refer to the Inventor API documentation found here as well as looking up various VBA tutorials and documentation if I have a general programming issue

0 Likes
Message 8 of 11

jholland3XDLM
Advocate
Advocate

So it accesses the hole diameter in that feature, not the parameter that was entered. I could name that specific hole as flangeHole and the Diameter as flangeBoltHoleDia but the syntax  oFeature.HoleDiameter.Value will still change the hole diameter even though I renamed the parameter?

 

When accessing it this way, the fact that it sees everything as cm becomes important. If i accessed the parameter directly it wouldn't matter. Then of course I would need the specific name of the parameter, eliminating the ability to loop thru all features. 

 

I still have a lot to learn!

0 Likes
Message 9 of 11

AdamAG99T
Advocate
Advocate

Yes, the benefit of accessing the parameter without explicitly referring to it by name is that you can access it from a loop or other more broad application. This is likely not the most efficient or cleanest way to do so but as a quick example showing how this would be used I made a rough example.

Dim oDoc As PartDocument = ThisApplication.ActiveDocument  'gets the active file
Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition 'provides access to the various features of the part file 
Dim oPartFeatures As PartFeatures = oCompDef.Features 'a collection of every feature in the specified part file

For Each oFeature In oPartFeatures 'runs through every feature in the part file
 If TypeOf oFeature Is HoleFeature Then 'checks if the feature is a hole
  If oFeature.HoleDiameter.Value=.3175 Then '.3175cm=.125in
   MessageBox.Show(oFeature.Name) 'prints the name of the hole feature when it has a diameter of .125 in
  End If
End If Next

 

0 Likes
Message 10 of 11

Anonymous
Not applicable

Hey probably just something silly but I'm still getting an error.

It says "Object variable or With block variable not set."

 

I'll paste my full code below I'm guessing I don't have the features defined correctly. Thanks!

 

Sub Main 
	 
	Dim oDoc As DrawingDocument 
	Dim oSheet As Sheet
	Dim oViews As DrawingViews
	Dim oView As DrawingView
	
	' Get the active drawing document.
	oDoc = ThisApplication.ActiveDocument 
	
	'call the sub to create layer
	Call CreateLayer
	
	'get current sheet so it can
	'be made active again later
	Dim oCurrentSheet As Sheet
	oCurrentSheet = oDoc.ActiveSheet	
	
	' Iterate through the sheets
	For Each oSheet In oDoc.Sheets
	'	activate the sheet
		oSheet.Activate
		
		' Iterate through the views on the sheet
		For Each oView In oSheet.DrawingViews			
			
			Dim docDesc As DocumentDescriptor 
			docDesc = oView.ReferencedDocumentDescriptor  
			
			' Verify that the drawing view is of part
			If docDesc.ReferencedDocumentType <> kPartDocumentObject Then 
				Continue For
			End If  
			
			' Get the component definition 
			Dim oCompDef As PartComponentDefinition 
			oCompDef = docDesc.ReferencedDocument.ComponentDefinition  
			
			' Process the view, wrapping it in a transaction so the 
			' each view can be undone with a single undo operation. 
			Dim trans As Transaction 
			trans = ThisApplication.TransactionManager.StartTransaction( _ 
									oDoc, "Change drawing view color")  
			
			' Call the sub to change colors
			Call ProcessColor(oView, oCompDef) 
			trans.End 
		Next
		'update the sheet
		oSheet.Update
	Next
	
	'return to original sheet	
	oCurrentSheet.Activate
End Sub 

Private Sub ProcessColor(drawView As DrawingView, _ 
                                oCompDef As ComponentDefinition) 
   	' Iterate through features
   	Dim occ As ComponentOccurrence 
	For Each oFeature In oCompDef.Features
		If TypeOf oFeature Is HoleFeature Then	
			If oFeature.HoleDiameter.Value = 0.65278 Then
			' Get all of the curves associated with the feature
			On Error Resume Next 
			Dim drawcurves As DrawingCurvesEnumerator 
			'drawcurves = drawView.DrawingCurves(occ) 
			drawcurves = drawView.DrawingCurves(oFeature) 
			If Err.Number = 0 Then 
				On Error GoTo 0  
		
				' Create an empty collection. 
				Dim objColl As ObjectCollection 
				objColl = ThisApplication.TransientObjects.CreateObjectCollection()  
		
				' Add the curve segments to the collection. 
				Dim drawCurve As DrawingCurve 
				For Each drawCurve In drawcurves 
					Dim segment As DrawingCurveSegment 
					For Each segment In drawCurve.Segments 
						objColl.Add (segment)
					Next 
				Next  
				
				Dim colorLayer As Layer 
				colorLayer = ThisDoc.Document .StylesManager.Layers.Item("Holes") 
		
				' Change the layer of all of the segments. 
				Call drawView.Parent.ChangeLayer(objColl, colorLayer) 
			End If 
			On Error GoTo 0 
		End If 'ends if hole feature
	End If
   	Next 'feature
End Sub

Sub CreateLayer 
	Dim oDoc As DrawingDocument 
	oDoc = ThisDoc.Document 

	' Verify that a layer exists for this color. 
	Dim layers As LayersEnumerator 
	layers = oDoc.StylesManager.Layers 

	On Error Resume Next 
	Dim colorLayer As Layer 
	colorLayer = layers.Item("Holes")  

	If Err.Number <> 0 Then 
		On Error GoTo 0 

		'set to magenta
		Dim newColor As color 
		newColor = ThisApplication.TransientObjects.CreateColor(255, 0, 255) 

		' Copy an arbitrary layer giving it the name 
		' of the render style. 
		colorLayer = layers.Item(1).Copy("Holes") 

		' the attributes of the layer to use the color, 
		' have a solid line type, and a specific width. 
		colorLayer.Color = newColor 
		colorLayer.LineType = kContinuousLineType 
		colorLayer.LineWeight = 0.02 
	End If 
	On Error GoTo 0  


End Sub

 

0 Likes
Message 11 of 11

AdamAG99T
Advocate
Advocate

That iLogic code works fine for me, I'm not getting that error at all with a few quick test files I made. Is there any chance you could upload a dummy set of files that causes the error, or at least a screenshot of the full error message. Did the code work without the extra addition to find the hole diameter? Also to be safe what version of inventor are you running?

0 Likes