Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Convert DXF file to 3d IPT part

Anonymous

Convert DXF file to 3d IPT part

Anonymous
Not applicable

Hi,

 

I have tried to tell in the video what I looking for.

https://autode.sk/2zbx9Am

 

All help will be appreciated,

 

0 Likes
Reply
892 Views
8 Replies
Replies (8)

AlexFielder
Advisor
Advisor

You can probably do it by copying the dwg geometry into a sketch like that, but it leaves a heap of un-constrained geometry if you do.

 

A better approach is to modify this API sample:

(here converted to iLogic)

 

Option explicit on

Sub main()
	Call ImportDWGIntoSketch()	
End Sub

Sub ImportDWGIntoSketch()
    ' Get the DWG translator.
    Dim oDWGTranslator As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}") 

    Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
        
    ' Specify a DWG file to import it into sketch
    oDataMedium.FileName = "C:\Temp\abc.dwg"
 
    Dim oTranslationContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
    oTranslationContext.Type = kFileBrowseIOMechanism
    
    Dim oDoc As PartDocument = ThisApplication.Documents.Add(kPartDocumentObject)
    
    ' Get the sketch that the DWG will be imported into.
    Dim oSk As PlanarSketch = oDoc.ComponentDefinition.Sketches.Add(oDoc.ComponentDefinition.WorkPlanes(3))

    ' Open the sketch for edit.
    oSk.Edit
    
    ' Specify the sketch to import the DWG into.
    oTranslationContext.OpenIntoExisting = oSk
    
    Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
    
    ' Specify the layers to import
    oOptions.Add( "SelectedLayers", "0")
    oOptions.Add ("InvertLayersSelection", True)

    ' Specify the units
    oOptions.Add ("FileUnits", "Centimeters")

    ' Set to constraint the end points.
    oOptions.Add ("ConstrainEndPoints", True)

    ' Do the translation.
    Call oDWGTranslator.Open(oDataMedium, oTranslationContext, oOptions, oDoc)
End Sub

The best approach possible could be to use the DWG Underlay facility and then project the resultant dwg geometry into a new sketch on the same plane, since it means the result is update-capable should the dwg change in future.

 

I've yet to find if there's an API method for the above however but will reply again shortly.

 

Cheers,

 

Alex.

0 Likes

AlexFielder
Advisor
Advisor

Okay, so there is an api sample here that when converted to iLogic imports a dwg straight into a new part file:

 

Sub AssociativelyImportAutoCADDWGSample()
    Dim oDoc As PartDocument = ThisApplication.Documents.Add(kPartDocumentObject)
    
    Dim oPartCompDef As PartComponentDefinition = oDoc.ComponentDefinition
    
    ' Create the ImportedDWGComponentDefinition bases on an AutoCAD file
    Dim oImportedDWGCompDef As ImportedDWGComponentDefinition = oPartCompDef.ReferenceComponents.ImportedComponents.CreateDefinition("F:\Onedrive For Business\OneDrive - GRAITEC\Inventor\Designs\Blog - Webinar\Inventor LT\companion cube..dwg")

    ' Import the AutoCAD DWG
    Dim oImportedComp As ImportedComponent = oPartCompDef.ReferenceComponents.ImportedComponents.Add(oImportedDWGCompDef)
End Sub

I pointed it at the Companion Cube dwg (from the game Portal) that I had laying around and it just works, giving you a new blank part file with the dwg imported.

 

The next step is to create a new sketch on the XY plane, and project the AutoCAD Geometry into it.

0 Likes

AlexFielder
Advisor
Advisor

Okay, so it took me a few moments to understand what I was reading, but this VBA once converted does the bulk of the work for us:

 

I modified it to allow you to pass a filename:

 

 

Option Explicit On

Sub main()
	Call CreateImportedDWGComponentSample("F:\Onedrive For Business\OneDrive - GRAITEC\Inventor\Designs\Blog - Webinar\Inventor LT\companion cube..dwg")
End Sub

Sub CreateImportedDWGComponentSample(ByVal filename As String)
    Dim oDoc As PartDocument = ThisApplication.Documents.Add(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 = 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
    
    If oImportedComponent.Type = ObjectTypeEnum.kImportedDWGComponentObject Then
        oImportedDWGComponent = oImportedComponent
        
        Dim oSk As PlanarSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))
        
        ' Get the DWGBlockDefinition for model space.
        Dim oDWGModelSpaceDef As DWGBlockDefinition = oImportedDWGComponent.ModelSpaceDefinition
        
        ' Project DWG entities to planar sketch.
        Dim oDWGEntity As DWGEntity
        For Each oDWGEntity In oDWGModelSpaceDef.Entities
         
            Call oSk.AddByProjectingEntity(oDWGEntity)
        Next
    End If
End Sub

All that's left is to run the extrude command.

 

🙂

0 Likes

Anonymous
Not applicable

Hi @AlexFielder,

 

Thanks for your reply, Can you help me out why I have so many red codelines?

2018-10-30_1114.png

 

0 Likes

AlexFielder
Advisor
Advisor

Easy peasy: The code I posted is iLogic, not VBA.

 

EDIT: here is a "diff" showing the differences between my rule and the original from the documentation:

 

https://www.diffchecker.com/13IixtxX

0 Likes

Anonymous
Not applicable

Hi @AlexFielder,

 

It should works for dxf also, Right?

2018-10-30_1114.png

 

What do i wrong, I get an error.

2018-10-30_1126_001.png2018-10-30_1126.png

 

I'm not sharp in iLogic, sorry.

 

0 Likes

AlexFielder
Advisor
Advisor

doesn't look like this API method supports .dxf files. Which is odd/bad.

0 Likes

AlexFielder
Advisor
Advisor

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.

 

0 Likes