Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
brendon.serrano
in reply to: sam

@sam No Problem! I have only been using iLogic for a couple months now, but I tend to enjoy coding. My code is a mixture of what I could find and what I could make work. I initially had the parameters in the code because I was using it as an internal rule. When an internal rule has a parameter in it called by only using its name (eg. "MyParam", FYI: You can only do this in internal rules), when this parameter is changed or edited the rule will trigger. I created a form to extrude a bar and I put those parameters in the form for the user to fill out. This way when they could simply fill out a length width and height on the form to quickly create a bar (I did this to make people draw off the origin as much as possible). Anyway, I scrapped the parameters as a trigger by calling them with "Parameter("MyParam")" and added the iLogic code as a button on the end of the form. My final product with this code was using a macro to create a button on the ribbon that would prompt the form and the user inputs desired values and hits create. A bar is then created on the origin planes with the input parameters as its dimensions. Still doesn't work perfectly. I have a bug where when the bar is already created and the parameters are changed, it will no longer be on the origin. I just haven't bothered fixing it yet. 

 

I moved all of my code over to external rules for ease of use on the network, and for legacy documents. Here is my full code and some screen shots of the process. Hope this helps, let me know if you have any other questions. 

 

Dim oDoc = ThisDoc.Document

If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
    ' Set a reference to the active sketch.    
    Dim OpenSketch As Sketch = ThisApplication.ActiveEditObject
    OpenSketch.ExitEdit
End If

Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition


' Get the X-Y work plane.
Dim xyPlane As WorkPlane
xyPlane = oDef.WorkPlanes.Item("XY Plane")

Try
	ExtrusionExists = ThisApplication.ActiveEditDocument.ComponentDefinition.Features("Bar Extrusion")
	ThisApplication.ActiveView.Fit

	Catch
		Try
			SketchExists = ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch")
			SketchExists.delete
			Catch
		End Try 


' Create a new sketch.
Dim sketch As Inventor.PlanarSketch
sketch = oDef.Sketches.Add(xyPlane, True)
sketch.Name = "Bar_Sketch"

ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").Edit


InventorVb.DocumentUpdate()


' Check to make sure a sketch is open.
If Not TypeOf ThisApplication.ActiveEditObject Is PlanarSketch Then
MessageBox.Show("A sketch must be active.", "iLogic")
Return
End If

'set a reference to the active sketch.
Dim oSketch As PlanarSketch
oSketch = ThisApplication.ActiveEditObject

'set a reference to the transient geometry collection.
Dim oTransGeom As TransientGeometry
oTransGeom = ThisApplication.TransientGeometry

' Create a rectangle 

width = Parameter("BAR_W") 'Trigger
height = Parameter("BAR_H") 'Trigger
length = Parameter("BAR_L") 'Trigger

Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle( oTransGeom.CreatePoint2d(0, 0),oTransGeom.CreatePoint2d(width*2.54/2, height*2.54/2))

Dim oSketchLine As Inventor.SketchLine
Dim oDim As DimensionConstraint

oSketchLine = oRectangleLines.Item(1) 

oDim = oSketch.DimensionConstraints.AddTwoPointDistance _
	(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, _
	DimensionOrientationEnum.kHorizontalDim, _
	oTransGeom.CreatePoint2d(0, -(width/2 )))

oDim.Parameter.Expression = "BAR_W"

oSketchLine = oRectangleLines.Item(2)

oDim = oSketch.DimensionConstraints.AddTwoPointDistance _
	(oSketchLine.StartSketchPoint, oSketchLine.EndSketchPoint, _
	DimensionOrientationEnum.kVerticalDim, _
	oTransGeom.CreatePoint2d(height+2, 0))

oDim.Parameter.Expression = "BAR_H"

ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").ExitEdit


Dim extrudeProfile As Profile = sketch.Profiles.AddForSolid 
Dim oExtFeature As ExtrudeFeature
oExtFeature = oDoc.ComponentDefinition.Features.ExtrudeFeatures.AddByDistanceExtent (extrudeProfile, "BAR_L", kSymmetricExtentDirection, kJoinOperation)
oExtFeature.Name = "Bar Extrusion"

ThisApplication.ActiveView.Fit
iLogicVb.UpdateWhenDone = True


End Try

 Extrude Bar Button and Form.png