rule to save an *iam

rule to save an *iam

Anonymous
Not applicable
1,002 Views
7 Replies
Message 1 of 8

rule to save an *iam

Anonymous
Not applicable
Hello ,
I do mechanical design and use a macro in vba to automate a process of generate iparts members and export them in step format . I hope to explain myself well .
This macro automatically a command to create some components one by one ( thousands ) , these components are then inserted in an assembly that has the extension * .iam and then thanks to this are converted automatically always in * .stp . my problem is that it works perfectly but currently the macro hangs upon creation of the file * .iam , asks me location and name for saving , then starts again . I wanted to ask if you can make the macro a name in the subject with a certainposition automatically ( so then once finished this process no longer need this file * .iam , precisely serves only to macro to convert all files created , usually then delete it ) . In this way I could do from the macro and the pc would all automatically start to finish .
I place the code and I highlight the save point where I would like the file * .iam was named and saved automatically .

Thank you in advance

Public Sub AddiPartOccurrence2()
' Open the factory document invisible.
Dim oFactoryDoc As PartDocument
Set oFactoryDoc = ThisApplication.Documents.Open("D:\chiavetta 21-12\disegni inventor\2\000progetto\parti\tra.ipt", False)

' Set a reference to the component definition.
Dim oCompDef As PartComponentDefinition
Set oCompDef = oFactoryDoc.ComponentDefinition

' Make sure we have an iPart factory.
If oCompDef.IsiPartFactory = False Then
MsgBox "Chosen document is not a factory.", vbExclamation
Exit Sub
End If

' Set a reference to the factory.
Dim oiPartFactory As iPartFactory
Set oiPartFactory = oCompDef.iPartFactory

' Get the number of rows in the factory.
Dim iNumRows As Integer
iNumRows = oiPartFactory.TableRows.Count

' Create a new assembly document
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.Documents.Add(kAssemblyDocumentObject, , True)

Dim oOccs As ComponentOccurrences
Set oOccs = oDoc.ComponentDefinition.Occurrences

Dim oPos As Matrix
Set oPos = ThisApplication.TransientGeometry.CreateMatrix

Dim oStep As Double
oStep = 0#
Dim iRow As Long

' Add an occurrence for each member in the factory.
For iRow = 1 To iNumRows

oStep = oStep + 10

' Add a translation along X axis
oPos.SetTranslation ThisApplication.TransientGeometry.CreateVector(oStep, oStep, 0)

Dim oOcc As ComponentOccurrence
Set oOcc = oOccs.AddiPartMember("D:\chiavetta 21-12\disegni inventor\2\000progetto\parti\tra.ipt", oPos, iRow)

Next

oDoc.Save '----------------thisis the Point where it ask me name and path of *.iam!!!
Dim oRefDoc As Document
For Each oRefDoc In oDoc.ReferencedDocuments
Call ExportToSTEP2(oRefDoc)
Next


End Sub

Public Sub ExportToSTEP2(oDoc As Document)

Dim exportPath As String
exportPath = "D:\chiavetta 21-12\disegni inventor\"

' Get the STEP translator Add-In.
Dim oSTEPTranslator As TranslatorAddIn
Set oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")

If oSTEPTranslator Is Nothing Then
MsgBox "Could not access STEP translator."
Exit Sub
End If

Dim oContext As TranslationContext
Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oOptions As NameValueMap
Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap
If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
' Set application protocol.
' 2 = AP 203 - Configuration Controlled Design
' 3 = AP 214 - Automotive Design
oOptions.Value("ApplicationProtocolType") = 3

' Other options...
'oOptions.Value("Author") = ""
'oOptions.Value("Authorization") = ""
'oOptions.Value("Description") = ""

oOptions.Value("Organization") = "Boeing SPA"

oContext.Type = kFileBrowseIOMechanism

Dim oData As DataMedium
Set oData = ThisApplication.TransientObjects.CreateDataMedium


'format file name
Dim FNamePos As Long
'postion of last back slash
FNamePos = InStrRev(oDoc.FullFileName, "\", -1)
Dim docFName As String
'file name with extension
docFName = Strings.Right(oDoc.FullFileName, Len(oDoc.FullFileName) - FNamePos)
'file name without extension
Dim shortName As String
shortName = Strings.Left(docFName, Len(docFName) - 4)

oData.FileName = exportPath & shortName & ".stp"

Call oSTEPTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
End If
End Sub
0 Likes
Accepted solutions (1)
1,003 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
For completeness I like that the macro at the step that I highlighted would save " oDoc " such as " C : \ New Folder \ prova.iam " , and if do not care whether to overwrite , that's all , so the macro would continue till the end automatically. But I am not able to do it.
Thank you For your time
0 Likes
Message 3 of 8

rossano_praderi
Collaborator
Collaborator

Hi Roberto,

 

' use SaveAs instead of Save

On Error Resume Next ' place this whereever you need

Dim oRefDoc As Document
For Each oRefDoc In oDoc.ReferencedDocuments
    Call ExportToSTEP2(oRefDoc)
Next


End Sub

Public Sub ExportToSTEP2(oDoc As Document)

........ ' I haven't found any error there
    
'format file name
'file name without extension
Dim shortName As String
shortName = Right(oDoc.FullFileName, Len(oDoc.FullFileName) - InStrRev(oDoc.FullFileName, "\"))
shortName = Left(shortName, Len(shortName) - 4)

oData.FileName = exportPath & shortName & ".stp"

..........

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 4 of 8

Anonymous
Not applicable

hi, tank you

but my macros work good, no errors. the macro generates all the members of an ipart, put them in a assembly, and then take all files of the assembly and exports to step format. My problem is not that it doesn't run or gives errors, but only that when it generates the assembly and put all iparts members there,when it save it ( oDoc.save ) it ask me path and name of this assembly. Then calls the macro export and make all very well.

I like write instead oDoc.save some code that give path and name of the assembly and save it automatically ,so the macro work alone from start till end.

es: oDoc.saveAS( "C:\assembly.iam")   , but I don't know how write it in VBA for inventor.

Could you help me please?

0 Likes
Message 5 of 8

Balaji_07
Advocate
Advocate

Hi Roberto,

Try to perform Silent operation for your macro by adding this line at the top of your rule

 

ThisApplication.SilentOperation = True

 

and do not forget to set it to False at the end of your rule.

 

Thanks,

Balaji A

 

Accept this post as Solution if  you found it usefull and do not forget to hit Kudos! :catwink:

0 Likes
Message 6 of 8

Anonymous
Not applicable
Hi,
I know that I have to use saveas instead save, but I don't know how write it, it give an error if I write
oDoc.saveas ("C:\assembly.iam") or something of similar. Could you write to me the correct expression please?
0 Likes
Message 7 of 8

Balaji_07
Advocate
Advocate
Accepted solution

Hi Roberto,

 

Hope the below syntax will help you!

 

Call odoc.SaveAs("Path&filename", True)

 

Thanks,

Balaji A

 

Accept this post as Solution if  you found it usefull and do not forget to hit Kudos! Smiley Wink

Message 8 of 8

Anonymous
Not applicable
BALAJI .... That Is the solution I need!!!! Thank you, I missed "Call" when I tryed! Thank you again!! Now this macro looks perfect!
Thank you
0 Likes