Here's the updated rule which correctly creates the extrusion and renames the extrusion height to "Thickness":
Option explicit on
'''
Sub main()
Call CreateImportedDWGComponentSample("F:\Onedrive For Business\OneDrive - GRAITEC\Inventor\Designs\Blog - Webinar\Inventor LT\companion cube..dwg")
'neither of these work with .dxfs
'Call CreateImportedDWGComponentSample("F:\Onedrive For Business\OneDrive - GRAITEC\Inventor\Designs\Inventor 2019\companion cube.dxf") 'F:\Onedrive For Business\OneDrive - GRAITEC\Inventor\Designs\Blog - Webinar\Inventor LT\companion cube..dwg")
'Call ImportDWGIntoSketch("F:\Onedrive For Business\OneDrive - GRAITEC\Inventor\Designs\Inventor 2019\companion cube.dxf")
End Sub
''' <summary>
''' This rule runs without error if there are no dimensions in the original .dwg file.
''' </summary>
''' <param name="filename"></param>
Sub CreateImportedDWGComponentSample(ByVal filename As String, Optional Thickness As String = "0.0")
Dim oDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject))
Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition
Dim oRefComponents As ReferenceComponents = oCompDef.ReferenceComponents
' Create a ImportedComponentDefinition based on an AutoCAD file.
Dim oImportedCompDef As ImportedComponentDefinition = oRefComponents.ImportedComponents.CreateDefinition(filename)
Dim oImportedDWGDef As ImportedDWGComponentDefinition
If oImportedCompDef.Type = ObjectTypeEnum.kImportedDWGComponentDefinitionObject Then
oImportedDWGDef = oImportedCompDef
Else
Exit Sub
End If
Dim oMatrix As Matrix = ThisApplication.TransientGeometry.CreateMatrix
oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(0, 0, 10))
oImportedDWGDef.Transformation = oMatrix
' Create the ImportedComponent
Dim oImportedComponent As ImportedComponent = oRefComponents.ImportedComponents.Add(oImportedDWGDef)
Dim oImportedDWGComponent As ImportedDWGComponent
Dim oSk As PlanarSketch = Nothing
If oImportedComponent.Type = ObjectTypeEnum.kImportedDWGComponentObject Then
oImportedDWGComponent = oImportedComponent
oSk = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))
' Get the DWGBlockDefinition for model space.
Dim oDWGModelSpaceDef As DWGBlockDefinition = oImportedDWGComponent.ModelSpaceDefinition
' Project DWG entities to planar sketch.
For Each oDWGEntity As DWGEntity In oDWGModelSpaceDef.Entities
If Not oDWGEntity.Type = ObjectTypeEnum.kDWGEntityProxyObject Or Not oDWGEntity.Type = ObjectTypeEnum.kDWGEntityObject Then
' If Not TypeOf(oDWGEntity) Is ObjectTypeEnum.kDWGEntityProxyObject Or Not TypeOf(oDWGEntity) Is ObjectTypeEnum.kDWGEntityObject Then
Call oSk.AddByProjectingEntity(oDWGEntity)
Else
Logger.Debug("DWG Entity Type: " & oDWGEntity.Type)
End If
Next
End If
oImportedDWGComponent.Visible = False
Dim newProfile As Profile = oSk.Profiles.AddForSolid
Dim regionProps As RegionProperties = newProfile.RegionProperties
regionProps.Accuracy = AccuracyEnum.kMedium
Dim oExtrudeDef As ExtrudeDefinition = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(newProfile, kJoinOperation)
If thickness = "0.0" Then
thickness = InputBox("Extrusion Thickness?", "Extrusion Thickness", "10 mm")
End If
Call oExtrudeDef.SetDistanceExtent(1, PartFeatureExtentDirectionEnum.kNegativeExtentDirection)
Dim oExtrude As ExtrudeFeature = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
oExtrude.SetEndOfPart(True)
Dim p As ModelParameter = getNewlyCreatedModelParam(oCompDef, True) ' minParam 'oCompDef.Parameters("d142") 'Parameter.Param("d142")
p.Name = "Thickness"
p.Expression = Thickness
oCompDef.SetEndOfPartToTopOrBottom(False)
End Sub
Function getNewlyCreatedModelParam(compDef As ComponentDefinition, minParam As Boolean) As ModelParameter
Dim ModelParamList As List(Of ModelParameter) = New List(Of ModelParameter)
For Each MParameter As ModelParameter In compDef.Parameters.ModelParameters
ModelParamList.Add(MParameter)
Next
Logger.Debug(ModelParamList.Count)
Dim Param As ModelParameter = Nothing
'Dim minParamNum As Integer = 0
If ModelParamList.Count > 0 Then
ModelParamList.Sort(Function(x As ModelParameter, y As ModelParameter) x.Name.CompareTo(y.Name))
If minParam Then
Param = (From tmpParam As ModelParameter In ModelParamList Select tmpparam).First()
Else
Param = (From tmpParam As ModelParameter In ModelParamList Select tmpparam).Last()
End If
End If
Return Param
End Function
NOTE: In its current form this iLogic rule is Inventor 2019-exclusive because I'm using LINQ to retrieve the first-created parameter from the resultant part component definition. If you're using an older version of Inventor it needs a few additions.
If you have a bunch of .dwg files that need to be processed, you can run this rule (inside a loop) from a parent file with a few additions/amendments.