Creating a Custom Task- Renaming Files

Creating a Custom Task- Renaming Files

amarinXG8V6
Advocate Advocate
1,209 Views
6 Replies
Message 1 of 7

Creating a Custom Task- Renaming Files

amarinXG8V6
Advocate
Advocate

Hi everyone,

 

I looking for a way to create  custom task within the Task Scheduler that will allow me to pick a folder directory and a file type (.IPT as an example) and export to .IGS as well as rename all of those files with a value that is stored within a user defined parameter within each part.

 

First I want to say that I don't know how I would go about creating a Program ID (COM) or an Application (EXE file). Is that something that has to be done in Visual Studio or .NET? 

 

amarinXG8V6_0-1648592375685.png

amarinXG8V6_1-1648592450679.png

 

amarinXG8V6_2-1648596111731.png

amarinXG8V6_3-1648596314690.png

amarinXG8V6_4-1648596351593.png

 

 

 

 

0 Likes
Accepted solutions (1)
1,210 Views
6 Replies
Replies (6)
Message 2 of 7

Ralf_Krieg
Advisor
Advisor

Hello

 

To create a custom task you need to create an exe program or a COM component. Both can be built with an IDE like Microsoft Visual Studio. You'll need a graphical interface too for selecting files, pick target folder and so on. Some work to do.

How many files do you need to export and how often?

Maybe it's an much easier way to:

- Create an assembly file and put all parts to export in it

- Grab an existing iLogic code to export a part to IGES

- Modify code to run through all parts in your assembly, modify filename, let you pick target folder

 

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
Message 3 of 7

amarinXG8V6
Advocate
Advocate

@Ralf_Krieg ,

 

Thank you for the feedback! I didn't consider that this could be done using iLogic. These components are always in an assembly and luckily they are in a "segregated" assembly because the parts are created using Frame Generator. Since Frame Generator creates it's own assembly with only the structural members placed using the Frame Generator tools, that would be the perfect place to launch the rule from. The number of components in the assembly can vary so some sort of loop function would need to be written in ilogic to run through and capture all of the components present in the assembly. All of what you said would make sense however I would like for the renaming of each file to be based on an existing user parameter that exists and is unique in each part. I don't want the user to specify the new name of each component but rather have to code run through all of the parts, find said "Part Tag" parameter and use that as the name for each file when it exports the parts.

 

I'm not well versed enough in iLogic to know what this type of code would look like. Would you happen to have an example?

 

Thank you for the help!

0 Likes
Message 4 of 7

Ralf_Krieg
Advisor
Advisor

Hello

 

A parameter "PART TAG" is not possible, cause whitespace is not allowed in parameter names. I wrote "PART_TAG" instead. At start a dialog for select the target folder is displayed. If the folder is always the same, we can replace this with a hard coded path to save time and clicks.

 

Option Explicit On
Imports System.Windows.Forms
Sub Main
	
	If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("Function available only in assemblies", MsgBoxStyle.Critical, "iLogic - Save as IGES")
		Exit Sub
	End If
	
	Dim openDoc As AssemblyDocument
	openDoc = ThisDoc.Document
	
	Dim oFileDlg As FolderBrowserDialog = New System.Windows.Forms.FolderBrowserDialog() 
	oFileDlg.SelectedPath=ThisDoc.WorkspacePath() 'Workspace path in active project
	oFileDlg.Description="Select target folder for IGES Export"
	
	Dim result As DialogResult = oFileDlg.ShowDialog
	Dim sfolder As String
	If result = DialogResult.OK Then
		sfolder = oFileDlg.SelectedPath
	Else
		MsgBox("Function aborted by user.", MsgBoxStyle.Information, "iLogic - Save as IGES")
		Exit Sub
	End If
	
	'Look at all of the files referenced in the open document
	Dim sfilename As String
	Dim docFile As Document
	Dim docPart As PartDocument
	Dim param As Inventor.Parameter
	For Each docFile In openDoc.AllReferencedDocuments                
		If docFile.DocumentType=DocumentTypeEnum.kPartDocumentObject Then
			docPart=DirectCast(docFile,PartDocument)
			
			Try
				param = docPart.ComponentDefinition.Parameters.UserParameters.Item("PART_TAG")
			Catch
				MsgBox("Parameter 'PART_TAG' not found in part " & docPart.DisplayName & vbCrLf & "Skipping this part and try next one.", MsgBoxStyle.Exclamation, "iLogic - Save as IGES")
				Continue For
			End Try
			
			sfilename = param.Value
	
			ExportIGES(docFile, sfolder, sfilename)
		End If
	Next

End Sub

Private Sub ExportIGES(ByVal oDoc As PartDocument, ByVal sFileName As String, sFolder As String)
	Dim oIGESTranslator As TranslatorAddIn
	oIGESTranslator = ThisApplication.ApplicationAddIns.ItemById _ 
	("{90AF7F44-0C01-11D5-8E83-0010B541CD80}")
	Dim oContext As TranslationContext
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim oOptions As NameValueMap
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	If oIGESTranslator.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
	   ' Set geometry type for wireframe.
	   ' 0 = Surfaces, 1 = Solids, 2 = Wireframe
	   'oOptions.Value("GeometryType") = 1
	   ' To set other translator values:
	   ' oOptions.Value("SolidFaceType") = n
	   ' 0 = NURBS, 1 = Analytic
	   ' oOptions.Value("SurfaceType") = n
	   ' 0 = 143(Bounded), 1 = 144(Trimmed)
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		Dim oData As DataMedium
		oData = ThisApplication.TransientObjects.CreateDataMedium
		oData.FileName =  sFolder & "\" & sFileName & ".igs"
		oIGESTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
	End If
End Sub

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 5 of 7

amarinXG8V6
Advocate
Advocate

@Ralf_Krieg 

 

Thank you so much for the code you provided! I have saved it in my external rules folder as BatchIGSFileExport rule. I did run into an issue when trying to run this rule out of an assembly. See screen shot below of the error. If you could help me determine what I've done wrong, I would greatly appreciate it.

 

amarinXG8V6_0-1649088993338.png

 

 

Text format of error in rule below.

 

Error in rule: BatchIGSFileExport, in document: Tube Frame Configurator.iam

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.TranslatorAddIn.SaveCopyAs(Object SourceObject, TranslationContext Context, NameValueMap Options, DataMedium TargetData)
at ThisRule.ExportIGES(PartDocument oDoc, String sFileName, String sFolder)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Message 6 of 7

Ralf_Krieg
Advisor
Advisor
Accepted solution

Hello

 

A real embarrassing mistake. In line 44 I call the ExportIGES Sub with folder and filename, but  the sub awaits both informations in reverse order. Have tested and should work now. Added a short "Done" message at end. Remove it when not needed.

 

Option Explicit On
Imports System.Windows.Forms
Sub Main
	
	If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("Function available only in assemblies", MsgBoxStyle.Critical, "iLogic - Save as IGES")
		Exit Sub
	End If
	
	Dim openDoc As AssemblyDocument
	openDoc = ThisDoc.Document
	
	Dim oFileDlg As FolderBrowserDialog = New System.Windows.Forms.FolderBrowserDialog() 
	oFileDlg.SelectedPath=ThisDoc.WorkspacePath() 'Workspace path in active project
	oFileDlg.Description="Select target folder for IGES Export"
	
	Dim result As DialogResult = oFileDlg.ShowDialog
	Dim sfolder As String
	If result = DialogResult.OK Then
		sfolder = oFileDlg.SelectedPath
	Else
		MsgBox("Function aborted by user.", MsgBoxStyle.Information, "iLogic - Save as IGES")
		Exit Sub
	End If
	
	'Look at all of the files referenced in the open document
	Dim sfilename As String
	Dim docFile As Document
	Dim docPart As PartDocument
	Dim param As Inventor.Parameter
	For Each docFile In openDoc.AllReferencedDocuments                
		If docFile.DocumentType=DocumentTypeEnum.kPartDocumentObject Then
			docPart=DirectCast(docFile,PartDocument)
			
			Try
				param = docPart.ComponentDefinition.Parameters.UserParameters.Item("PART_TAG")
			Catch
				MsgBox("Parameter 'PART_TAG' not found in part " & docPart.DisplayName & vbCrLf & "Skipping this part and try next one.", MsgBoxStyle.Exclamation, "iLogic - Save as IGES")
				Continue For
			End Try
			
			sfilename = param.Value

			ExportIGES(docFile, sfilename, sfolder)
		End If
	Next

	MsgBox("Done",MsgBoxStyle.Information,"iLogic - Export to IGES")
End Sub

Private Sub ExportIGES(ByVal oDoc As PartDocument, ByVal sFileName As String, sFolder As String)
	Dim oIGESTranslator As TranslatorAddIn
	oIGESTranslator = ThisApplication.ApplicationAddIns.ItemById _ 
	("{90AF7F44-0C01-11D5-8E83-0010B541CD80}")
	Dim oContext As TranslationContext
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	Dim oOptions As NameValueMap
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	If oIGESTranslator.HasSaveCopyAsOptions(oDoc, oContext, oOptions) Then
	   ' Set geometry type for wireframe.
	   ' 0 = Surfaces, 1 = Solids, 2 = Wireframe
	   'oOptions.Value("GeometryType") = 1
	   ' To set other translator values:
	   ' oOptions.Value("SolidFaceType") = n
	   ' 0 = NURBS, 1 = Analytic
	   ' oOptions.Value("SurfaceType") = n
	   ' 0 = 143(Bounded), 1 = 144(Trimmed)
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
		Dim oData As DataMedium
		oData = ThisApplication.TransientObjects.CreateDataMedium
		oData.FileName =  sFolder & "\" & sFileName & ".igs"
		oIGESTranslator.SaveCopyAs(oDoc, oContext, oOptions, oData)
	End If
End Sub

 

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes
Message 7 of 7

amarinXG8V6
Advocate
Advocate

@Ralf_Krieg,

 

You are a master! Thank you so very much. It worked like a charm.

0 Likes