Create Macro to create assembly file, and place currently open part file into it grounded at origin.

Create Macro to create assembly file, and place currently open part file into it grounded at origin.

C_Haines_ENG
Collaborator Collaborator
825 Views
8 Replies
Message 1 of 9

Create Macro to create assembly file, and place currently open part file into it grounded at origin.

C_Haines_ENG
Collaborator
Collaborator

As the title says, I need to create about 2000 assembly files from parts so I can insert all of the hardware that goes into it, Is it possible to write a macro to create an assembly file and place the current part file I have open into it and grounded at the origin. 

0 Likes
Accepted solutions (1)
826 Views
8 Replies
Replies (8)
Message 2 of 9

Michael.Navara
Advisor
Advisor

This is what do you request. But you can create assembly occurrence by file name instead of ComponentDefinition.

Sub main
	Dim part As PartDocument = ThisDoc.Document
	Dim asmTemplate As String = "" 'Your assembly template file here
	Dim visible As Boolean = True
	
	Dim asm As AssemblyDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, asmTemplate, visible)
	
	Dim position As Matrix = ThisApplication.TransientGeometry.CreateMatrix()
	
	Dim occ As ComponentOccurrence = asm.ComponentDefinition.Occurrences.AddByComponentDefinition(part.ComponentDefinition, position)
	occ.Grounded = True
End Sub

 

0 Likes
Message 3 of 9

C_Haines_ENG
Collaborator
Collaborator

This doesn't seem to work with Macros, giving me a Syntax error. 

0 Likes
Message 4 of 9

Michael.Navara
Advisor
Advisor
0 Likes
Message 5 of 9

C_Haines_ENG
Collaborator
Collaborator

I am looking to get it to run with a keypress, are you able to convert it to VBA?

0 Likes
Message 6 of 9

Michael.Navara
Advisor
Advisor
Sub IC_11231683()
    Dim part As PartDocument
    Set part = ThisApplication.ActiveDocument
    Dim asmTemplate As String
    asmTemplate = "" 'Your assembly template file here
    Dim visible As Boolean
    visible = True
    
    Dim asm As AssemblyDocument
    Set asm = ThisApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, asmTemplate, visible)
    
    Dim position As Matrix
    Set position = ThisApplication.TransientGeometry.CreateMatrix()
    
    Dim occ As ComponentOccurrence
    Set occ = asm.ComponentDefinition.Occurrences.AddByComponentDefinition(part.ComponentDefinition, position)
    occ.Grounded = True
End Sub
0 Likes
Message 7 of 9

C_Haines_ENG
Collaborator
Collaborator

Honestly, Thank you So much you're saving me hours of work. Id hate to ask further but do you know if its possible to transfer the iProperty description of the part file to the assembly file? Aswell, is it possible to name it whatever the Part Number is?

0 Likes
Message 8 of 9

Michael.Navara
Advisor
Advisor
Accepted solution
Sub IC_11231683()
    Dim part As PartDocument
    Set part = ThisApplication.ActiveDocument
    
    Dim asmTemplate As String
    asmTemplate = ThisApplication.FileManager.GetTemplateFile(kAssemblyDocumentObject, kMetricSystemOfMeasure) 'Your assembly template file here
    
    Dim visible As Boolean
    visible = True
    
    Dim asm As AssemblyDocument
    Set asm = ThisApplication.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject, asmTemplate, visible)
    
    Dim position As Matrix
    Set position = ThisApplication.TransientGeometry.CreateMatrix()
    
    Dim occ As ComponentOccurrence
    Set occ = asm.ComponentDefinition.Occurrences.AddByComponentDefinition(part.ComponentDefinition, position)
    occ.Grounded = True
    
    Dim designTrackingProperties As String
    designTrackingProperties = "{32853F0F-3444-11D1-9E93-0060B03C1CA6}"
    
    Dim partNumber As String
    partNumber = part.PropertySets(designTrackingProperties)("Part Number").value
    
    Dim description As String
    description = part.PropertySets(designTrackingProperties)("Description").value
    
    'Set DisplayName to unsaved file becomes the default file name of this file.
    asm.PropertySets(designTrackingProperties)("Description").value = description
    asm.DisplayName = partNumber
    
    
End Sub
0 Likes
Message 9 of 9

C_Haines_ENG
Collaborator
Collaborator

This is Perfect! You're amazing!

0 Likes