Import sketched blocks from template

Import sketched blocks from template

GeorgK
Advisor Advisor
656 Views
3 Replies
Message 1 of 4

Import sketched blocks from template

GeorgK
Advisor
Advisor

Hello together,

 

how could I import sketched blocks from a template file?

 

Thanks

 

Georg

0 Likes
Accepted solutions (1)
657 Views
3 Replies
Replies (3)
Message 2 of 4

xiaodong_liang
Autodesk Support
Autodesk Support

Hi Georg,

I am not sure what kind of template file you meant by, but with API, you can firstly define a SketchBlockDefinition, and insert it as a SketchBlock. There is a sample code in API documentation (<Inventor>\Local Help\admapi_20_0.chm)

 

Create and insert a sketch block definition into a part sketch API Sample. I posted here for your convenience:

 

Public Sub CreateSketchBlockDefinition()
    ' Set a reference to the part document.
    ' This assumes a part document is active.
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument

    ' Create the new sketch block definition.
    Dim oSketchBlockDef As SketchBlockDefinition
    Set oSketchBlockDef = oPartDoc.ComponentDefinition.SketchBlockDefinitions.Add("My Block Def")

    ' Set a reference to the transient geometry object.
    Dim oTransGeom As TransientGeometry
    Set oTransGeom = ThisApplication.TransientGeometry

    ' Draw a 4cm x 3cm rectangle with the corner at (0,0)
    Dim oRectangleLines As SketchEntitiesEnumerator
    Set oRectangleLines = oSketchBlockDef.SketchLines.AddAsTwoPointRectangle( _
                                oTransGeom.CreatePoint2d(0, 0), _
                                oTransGeom.CreatePoint2d(4, 3))

End Sub

Public Sub InsertSketchBlockDefinition()

    ' Set a reference to the part document.
    ' This assumes a part document is active.
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument

    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition

    ' Create a new sketch on the X-Y work plane.
    Dim oSketch As PlanarSketch
    Set oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes(3))

    ' Set a reference to the definition named "My Block Def"
    Dim oSketchBlockDef As SketchBlockDefinition
    Set oSketchBlockDef = oCompDef.SketchBlockDefinitions.Item("My Block Def")

    Dim oPosition As Point2d
    Set oPosition = ThisApplication.TransientGeometry.CreatePoint2d(10, 10)

    ' Insert the sketch block definition
    Call oSketch.SketchBlocks.AddByDefinition(oSketchBlockDef, oPosition)
End Sub
0 Likes
Message 3 of 4

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi GeorgK,

 

Here is the iLogic rule that I use to do this.

 

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

 

'check file type 
If ThisDoc.Document.DocumentType <> kDrawingDocumentObject Then
Return
End If

'identitfy the source template file
Dim SourceFile As String = "\\server\departments\CAD\Inventor\Templates\Standard.idw" 

'define the drawing document
Dim oDrawDoc As DrawingDocument 
oDrawDoc = ThisApplication.ActiveEditDocument

'get the current browser pane name
Dim oCurrentPane As String
oCurrentPane = ThisApplication.ActiveDocument.BrowserPanes.ActivePane.Name

'make sure the current browser pane is not the Vault pane to avoid error
If oCurrentPane = "Vault" Then
ThisApplication.ActiveDocument.BrowserPanes.Item("Model").Activate
End If

'get the current sheet
Dim oCurrentNumber  As Sheet
oCurrentNumber = oDrawDoc.ActiveSheet

'delete symbols not in use
For Each oSymbol In oDrawDoc.SketchedSymbolDefinitions
	If oSymbol.IsReferenced = False Then
	oSymbol.Delete()
	End If	
Next	

'create list of symbols in use 
Dim oExistingSymbolsList As New ArrayList
For Each oExistingSymbol In oDrawDoc.SketchedSymbolDefinitions
	oExistingSymbolsList.add(oExistingSymbol.Name)
Next

'Open the template         
Dim strSourceIDW As DrawingDocument
strSourceIDW = ThisApplication.Documents.Open(SourceFile, False)

'iterate through the symbols
For Each oSymbol In strSourceIDW.SketchedSymbolDefinitions
	'look to see if the symbol is in the "in use" list
	If oExistingSymbolsList.Contains(oSymbol.Name) Then
		'do nothing
	Else
		' Get the new definitions.
		Dim oNewSymbol As SketchedSymbolDefinition
		oNewSymbol = strSourceIDW.SketchedSymbolDefinitions.Item(oSymbol.Name)
		'place the definitions in the drawing resources folder
		Dim oNewSymbolDef As SketchedSymbolDefinition
		oNewSymbolDef = oNewSymbol.CopyTo(oDrawDoc)
	End If
Next 

'activate the original current sheet
oCurrentNumber.activate 

'close the template/source file
strSourceIDW.Close()

EESignature

Message 4 of 4

GeorgK
Advisor
Advisor

Hello Curtis,

 

thank you very much. That's what I am looking for.

 

Georg

0 Likes