Create iLogic Extrusion and Drive Dimensions with Parameters

Create iLogic Extrusion and Drive Dimensions with Parameters

brendon.serrano
Enthusiast Enthusiast
1,959 Views
3 Replies
Message 1 of 4

Create iLogic Extrusion and Drive Dimensions with Parameters

brendon.serrano
Enthusiast
Enthusiast

I am using Inventor 2019 4.4. I am currently making a template for standard parts. I am trying to create a button that will automatically extrude either a rectangle or a round at the origin so that the origin planes with always be at the center of the extrusion. I have been able to get the extrusion and the sketch to be created, but I run into issues when I want to edit the extrusion to be a different size. So far I have made it so that the extrusion is deleted if it already exists. I am hoping to add the functionality that the extrusion will only be edited so if there are other features on the part. they will not need to be re-made. I was hoping I could drive the extrusion with parameters, but i cannot find a way to put the parameter in as the extrude length, or drive the sketch with parameters (except for the initial creation). Below is the code I have so far. If anyone has any ideas, or any information that might help me out, please let me know. The code below is for the rectangular bar only and i have a separate code for the round, but I may want to put them both in the same function down the road. 

 

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")
	ExtrusionExists.delete


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

		

'Try 
'	ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").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 = BAR_W
'height = BAR_H
'length = BAR_L

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

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, Parameter("BAR_L")*2.54, kSymmetricExtentDirection, kJoinOperation)
oExtFeature.Name = "Bar Extrusion"

ThisApplication.ActiveView.Fit
iLogicVb.UpdateWhenDone = True
0 Likes
Accepted solutions (3)
1,960 Views
3 Replies
Replies (3)
Message 2 of 4

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @brendon.serrano 

 

Here's a quick example based on what you had.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

sSketchName = "Bar_Sketch"
sExtName = "Bar Extrusion"

Dim oDoc As Document 
oDoc = ThisDoc.Document
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition

Dim oSketch As Inventor.PlanarSketch

oW = InputBox("Enter new Width", "iLogic", BAR_H)
oL = InputBox("Enter new Length", "iLogic", BAR_L)
oH = InputBox("Enter new Height", "iLogic", BAR_H)

Try 
	oExtrusion = oDef.Features(sExtName)
	GoTo _Found_Extrusion
Catch
End Try

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


' Create a new sketch.
oSketch = oDef.Sketches.Add(xyPlane, True)
oSketch.Name = sSketchName
oSketch.Edit


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

'' Create a rectangle 
Dim oRectangleLines As SketchEntitiesEnumerator
oRectangleLines = oSketch.SketchLines.AddAsTwoPointCenteredRectangle _
(oTransGeom.CreatePoint2d(0, 0), oTransGeom.CreatePoint2d(oW / 2, oL / 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, -(oW/2 )))

oDim.Parameter.Name = "BAR_L"

oSketchLine = oRectangleLines.Item(2)

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

oDim.Parameter.Name = "BAR_W"

oSketch.ExitEdit

Dim extrudeProfile As Profile 
extrudeProfile = oSketch.Profiles.AddForSolid 

Dim oExtFeature As ExtrudeFeature
oExtFeature = oDoc.ComponentDefinition.Features.ExtrudeFeatures.AddByDistanceExtent _
	(extrudeProfile, oH, _
	SymmetricExtentDirection, kJoinOperation)

oExtFeature.Name = "Bar Extrusion"
oExtFeature.Parameters.Item(4).Name = "BAR_H"


_Found_Extrusion :


BAR_W = oW
BAR_L = oL
BAR_H = oH

ThisApplication.ActiveView.Fit
iLogicVb.UpdateWhenDone = True

 

 

 

EESignature

0 Likes
Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

Did you know that when you place a driving dimensional constraint in a model sketch, you can specify a name for the Parameter it will be stored in.   For example you could enter "HEIGHT = 12" (without the quotes) instead of just a number, within the dimension, when placing it.  Then you can find that Parameter within the Parameters dialog box.  It will be one of the Model Parameters.

You can use the same logic within your code, to specify the name of the parameter that a feature is going to create, where specifying a dimension.  Just specify oHeight = "HEIGHT = 12" (with the quotes around the part after =  sign.

That's just for if you haven't already created UserParameters before-hand.

If you have already set-up UserParameters, you can just specify the name of the Parameter with a line like "oHeight = Parameter("HEIGHT").Value

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

brendon.serrano
Enthusiast
Enthusiast
Accepted solution

I was able to utilized both of your suggestions for the solution. I could not just simply set the "Parameter" in when creating the sketch rectangle, but it worked well for the extrusion. I also had to change how the dimensions were named in the sketch from parameter.name to parameter.Expression. my solution can be seen below. Thank you both for your quick responses and suggestions. 

 

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")
'	ExtrusionExists.delete
	width = BAR_W
	height = BAR_H
	length = BAR_L

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

		

'Try 
'	ThisApplication.ActiveEditDocument.ComponentDefinition.Sketches("Bar_Sketch").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 = BAR_W
height = BAR_H
length = BAR_L

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.Name = "BAR_W"
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
0 Likes