Import SketchBlock in part from templatefile

Import SketchBlock in part from templatefile

machiel.veldkamp
Collaborator Collaborator
694 Views
4 Replies
Message 1 of 5

Import SketchBlock in part from templatefile

machiel.veldkamp
Collaborator
Collaborator

Hi. 

 

 

I want to make a .ipt with all the SketchBlocks we need. 

 

When in a new part you can select "BlockImporter" and select the correct block that you'd need in that part. 

 

For instance I want to import the Block IPE500 and SQUARETUBE50x50x5 from the IPT file. 

I don't want to derive the template-ipt I just want to import the sketchblocks. 

 

 

Where to start? I've browsed the API for some references but all I can find there and on the internet is how to import Blocks in .idw's. That's great and all. But not what I want. 

 

 

Can this be done? So yes; I'd love some tips and perhaps a piece of the puzzle. I can figure most stuff out on my own by now but this... Help! 🙂

 

Thanks for listening

 

 

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
Accepted solutions (2)
695 Views
4 Replies
Replies (4)
Message 2 of 5

machiel.veldkamp
Collaborator
Collaborator
I found this!

http://forums.autodesk.com/t5/inventor-customization/api-copying-a-sketchblockdefinition-from-one-pa...

Gonna play with that for a bit.

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
0 Likes
Message 3 of 5

machiel.veldkamp
Collaborator
Collaborator
Alright. I'm on break in a bit. If anyone wan'ts to help out that'd be great.

Sub Main()
' Somehow get a reference to the document you want to copy from and the
' one you're copying to.
Dim oSourceFile As String = "C:\Relco\Vault\Designs\TESTPART1BLOCK.IPT"
Dim sourceDoc As PartDocument
Dim targetDoc As PartDocument
Dim oIndex = 1 'Counter
targetDoc = ThisApplication.ActiveDocument
sourceDoc = ThisApplication.Documents.Open(oSourceFile)

Dim BlockList As New ArrayList


' Get the sketch block definition you want to copy.
'Dim sourceBlockDef As String
'Dim sourceBlockList As SketchBlockDefinition
Dim oComponent = sourceDoc.ComponentDefinition
Dim SketchBlocks = oComponent.SketchBlockDefinitions
sourceBlockDef = SketchBlocks.Item(oIndex)
sourceBlockList = SketchBlocks.Count()
Dim SketchName = SketchBlocks.Name(1)

For Each sourceBlockDef In SketchBlocks
Try
MessageBox.Show("Show the name of the block" & oIndex, "Title")
oIndex = oIndex + 1
BlockList.Add("Block: "& SketchName & " Dinges")
Catch
MessageBox.Show("FAIL", "Title")
End Try
Next



'Copy the sketch block definition into the other document.
'Call sourceBlockDef.CopyTo(targetDoc)


End Sub





It doen'st work yet

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
0 Likes
Message 4 of 5

frederic.vandenplas
Collaborator
Collaborator
Accepted solution

Hi,

 

You can try this dinges Smiley Tongue

Dim targetDoc As PartDocument
targetDoc = ThisApplication.ActiveDocument

Dim sourceDoc As PartDocument
sourceDoc = ThisApplication.Documents.Open("C:\part1.ipt")

Dim oComponent As PartComponentDefinition
oComponent = sourceDoc.ComponentDefinition

Dim SketchBlockDef As SketchBlockDefinition
For Each SketchBlockDef In oComponent.SketchBlockDefinitions
    If SketchBlockDef.Name = "Block1" Then
        SketchBlockDef.CopyTo (targetDoc)
    End If
Next SketchBlockDef
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 5 of 5

machiel.veldkamp
Collaborator
Collaborator
Accepted solution

Allrrrrright! Here we go. This is what i wanted. Feel free to copy

 

SyntaxEditor Code Snippet

Sub Main()

    Dim targetDoc As PartDocument
    targetDoc = ThisApplication.ActiveDocument
    
    Dim sourceDoc As PartDocument
    sourceDoc = ThisApplication.Documents.Open("C:\TEMP\InventorBlockTemplate.ipt", False) 'Open File in background
    
    Dim oComponentSource As PartComponentDefinition
    oComponentSource = sourceDoc.ComponentDefinition
    
    Dim oComponentTarget As PartComponentDefinition
    oComponentTarget = targetDoc.ComponentDefinition
    
    
    Dim oIndex = 0 'Integer to count available blocks in SourceDoc
    Dim oIndexCur = 0 'Integer to count available blocks in TargetDoc
    
    
    Dim BlockList As New ArrayList 'Array to list all avalable Blocks SourceDoc
    Dim BlockListCur As New ArrayList 'Array to list all avalable Blocks TargetDoc
    
    'Put Available Blocks from SourceDoc in an ArrayList
    Dim SketchBlockDef As SketchBlockDefinition 
    For Each SketchBlockDef In oComponentSource.SketchBlockDefinitions
        Blocklist.Add(SketchBlockDef.Name)
        oIndex  = oIndex + 1
    Next SketchBlockDef
    
    'Put Available Blocks from TargetDoc in an ArrayList
    Dim SketchBlockDefCur As SketchBlockDefinition
    For Each SketchBlockDefCur In oComponentSource.SketchBlockDefinitions
        BlocklistCur.Add(SketchBlockDefCur.Name)
        oIndexCur  = oIndexCur + 1
    Next SketchBlockDefCur
    
    'Choose what Block to Import
    BlockChooser = InputListBox("CHOOSE BLOCK TO IMPORT", Blocklist, Blocklist, Title := "Block Importer", ListName := "Available Blocks")
    
    'Compare SourceDoc's Blocklist against TargetDoc's Blocklist
    For Each SketchBlockDef In oComponentSource.SketchBlockDefinitions
        If SketchBlockDef.Name = BlockChooser Then
            'Check if TargetDoc Contains this Block
            For Each SketchBlockDefCur In oComponentTarget.SketchBlockDefinitions
                If SketchBlockDefCur.Name = BlockChooser Then
                    MessageBox.Show("Block Already Imported, Exiting", "Error on Import",MessageBoxButtons.OK, MessageBoxIcon.STOP)
                    Exit Sub
                End If
            Next     SketchBlockDefCur
            SketchBlockDef.CopyTo (targetDoc)
        End If
    Next SketchBlockDef
    
    
End Sub

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
0 Likes