modify Search from alldirectories to 1 defined directory

modify Search from alldirectories to 1 defined directory

johan.degreef
Advisor Advisor
497 Views
3 Replies
Message 1 of 4

modify Search from alldirectories to 1 defined directory

johan.degreef
Advisor
Advisor

I have this code which searches through my library (with subfolders) and places the part in the project in the folder where the assembly resides. Due to performance reason I would like to place all library parts in the same folder. "MyProject\PTN_LIBRARY"

 

I know nothing about coding, but would it be difficult for the guru's here to make this code only use 1 folder to place the ibrary  parts?

So looking for the custom library part is in subfolders

Placing and looking for previous identical parts should be in 1 folder

 

 

Imports System.IO
GetInput :

Six_Digit = InputBox("Enter 6 Digit number", "iLogic", 000000)

If Six_Digit = "" Then
	Return
Else If Len(Six_Digit) <> "6" Then
MessageBox.Show("Input must be 6 digits", "ilogic")
GoTo GetInput

End If


Dim oDoc As Document
Dim sFilename As String

'hard code path
oLibrary_Folder = "C:\PTN_Part_Library\"


Dim oFilenames() As String
oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, _
"*.*", SearchOption.AllDirectories)

For Each oFilename As String In oFilenames
	If oFilename.Contains(Six_Digit) Then
		Dim oOptions As Inventor.NameValueMap
		oOptions = ThisApplication.TransientObjects.CreateNameValueMap
		oDoc = ThisApplication.Documents.OpenWithOptions(oFilename, oOptions, False)
		sFilename = oFilename
		Exit For
	End If
Next

If sFilename = "" Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If

iCounter = 0

'path from current file
oActiveAssemblyfolder = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
oProjectfolder = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	If oFilename.Contains(Six_Digit) Then
		iCounter = iCounter + 1
	End If
Next

'increment counter
iCounter = iCounter + 1


'find the postion of the last backslash in the path
FNamePos = InStrRev(sFilename, "\", - 1)
'get the file name with the file extension
oName = Right(sFilename, Len(sFilename) - FNamePos)
'get the file name (without extension)
ShortName = Left(oName, Len(oName) - 4)
'get extension
oExt = Right(oName, 4)


If iCounter < 10 Then
	iCounter = "00" + CStr(iCounter)
ElseIf iCounter < 100 Then
	iCounter = "0" + CStr(iCounter)
Else
	iCounter = CStr(iCounter)
End If

oNewName = ShortName & "_" & iCounter

oPathandName = oActiveAssemblyfolder & oNewName & oExt

'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)


'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent,oPathandName) 
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")

 

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes
498 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Just change the "SearchOption.AllDirectories" to "SearchOption.TopDirectoryOnly" at the end of your GetFiles call.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

Michael.Navara
Advisor
Advisor

Here is modified part of your code for searching in top directory only and better performance

 

'Inputs from code above
Dim Six_Digit As String = "000000"
Dim oLibrary_Folder = "C:\PTN_Part_Library\"

'Search files 
Dim searchPattern As String = String.Format("*{0}*.*", Six_Digit)
Dim oFilenames = System.IO.Directory.GetFiles(oLibrary_Folder, searchPattern, System.IO.SearchOption.TopDirectoryOnly)

If oFilenames.Length = 0 Then
	MessageBox.Show("No matching libary file found.", "iLogic")
	Return
End If

Dim sFilename = oFilenames(0)'First element in array

'TEST
For Each fileName As String In oFilenames
	Logger.Debug(fileName)
Next

 

 

Message 4 of 4

johan.degreef
Advisor
Advisor

Many thanks, but searching for the library part is in multiple directories, that is OK, so i tought it was the second part of the code that needs modifications?

Looking if the part already exists over all directories, should be changed to "project\PTN_LIBRARY" path 

Copying the part to the project, should be changed from "oPathAndName" to "project\PTN_LIBRARY" path

 

 

 

 

iCounter = 0

'path from current file
oActiveAssemblyfolder = ThisDoc.Path & "\"

'path from current project file ( *.ipj)
oProjectfolder = _
ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath & "\"

Dim oProject_Filenames() As String
oProject_Filenames = System.IO.Directory.GetFiles(oProjectfolder, _
"*.*", SearchOption.AllDirectories)

'count existing project files					  
For Each oFilename As String In oProject_Filenames
	If oFilename.Contains(Six_Digit) Then
		iCounter = iCounter + 1
	End If
Next

'increment counter
iCounter = iCounter + 1


'find the postion of the last backslash in the path
FNamePos = InStrRev(sFilename, "\", - 1)
'get the file name with the file extension
oName = Right(sFilename, Len(sFilename) - FNamePos)
'get the file name (without extension)
ShortName = Left(oName, Len(oName) - 4)
'get extension
oExt = Right(oName, 4)


If iCounter < 10 Then
	iCounter = "00" + CStr(iCounter)
ElseIf iCounter < 100 Then
	iCounter = "0" + CStr(iCounter)
Else
	iCounter = CStr(iCounter)
End If

oNewName = ShortName & "_" & iCounter

oPathandName = oActiveAssemblyfolder & oNewName & oExt

'save new document
oDoc.SaveAs(oPathandName, True)
oDoc.Close

'[ Place Component
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Create Matrix
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Call oMatrix.SetTranslation(ThisApplication.TransientGeometry.CreateVector(50, 50, 50), True)


'insert new occurence
'Dim oOcc As ComponentOccurrence
'oOcc = oAsmCompDef.Occurrences.Add( _
'oPathandName, oMatrix)

ThisApplication.CommandManager.PostPrivateEvent(PrivateEventTypeEnum.kFileNameEvent,oPathandName) 
ThisApplication.CommandManager.ControlDefinitions.Item("AssemblyPlaceComponentCmd").Execute


'oOcc.Grounded = False
'ThisDoc.Document.SelectSet.Select(oOcc)

']

'MessageBox.Show("New file created: " & vbLf & oPathandName, "iLogic")

 

 

 


 

Inventor 2025, Vault Professional 2025, Autocad Plant 3D 2025
0 Likes