Tuesday, July 3, 2012

iLogic: Get File Information for the Selected Component





Issue:
You want to find the file name or path of a selected component in your iLogic rule.

Solution:
Here is a quick iLogic snippet that will demonstrate how to work with the selected component file and path. In this example the file name, file name without extension, file path, and the full path and file name are written to a message box.




'------------ start of ilogic--------------------
'get currently selected component
Dim oOccurrence as ComponentOccurrence
Try
  oOccurrence = ThisDoc.Document.SelectSet.Item(1)
Catch
  MessageBox.Show("Please select a component before running this rule.", "iLogic")
  Return
End Try

Dim doc As Document
Dim CurFileName As String

'set the selected item
oOccurrence = ThisApplication.ActiveDocument.SelectSet.Item(1)

'get the selected item document occurrence name
doc = oOccurrence.Definition.Document

'get the path and file name of the selected item
CurFileName = doc.FullFileName

'defines backslash as the subdirectory separator
Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar

'find the postion of the last backslash in the path
FNamePos = InStrRev(CurFileName, "\", -1)   
'get the file name with the file extension
Name = Right(CurFileName, Len(CurFileName) - FNamePos)
'get the file name (without extension)
ShortName = Left(Name, Len(Name) - 4)
'get the path of the folder containing the file
Folder_Location = Left(CurFileName, Len(CurFileName) - Len(Name))

MessageBox.Show("File Name: " & Name _
& vblf & "File Name without extension: " & ShortName _
& vblf & "File Path: " & Folder_Location _
& vblf & "Path and File Name: " & CurFileName, "iLogic")
'------------ end of ilogic--------------------

Monday, July 2, 2012

iLogic and the Inventor Project File



Issue:
You want to reference the current Inventor Project file within an iLogic rule.

Solution:
Here is a quick iLogic snippet that will demonstrate how to work with the Project file and path. In this example the project name, project file name, project path, and the full project path and file name are written to a message box.





'------ start of iLogic ----------------------------------------------------------

Dim IPJ as String
Dim IPJ_Name as String
Dim IPJ_Path as String
Dim FNamePos As Long
'set a reference to the FileLocations object.
IPJ = ThisApplication.FileLocations.FileLocationsFile
'get the location of the last backslash seperator
FNamePos = InStrRev(IPJ, "\", -1)    
'get the project file name with the file extension
IPJ_Name = Right(IPJ, Len(IPJ) - FNamePos)
'get the project name (without extension)
IPJ_ShortName = Left(IPJ_Name, Len(IPJ_Name) - 4)
'get the path of the folder containing the project file
IPJ_Folder_Location = Left(IPJ, Len(IPJ) - Len(IPJ_Name))

MessageBox.Show("Project Name: " & IPJ_ShortName _
& vblf & "Project File Name: " & IPJ_Name _
& vblf & "Project Path: " & IPJ_Folder_Location _
& vblf & "Project Path and File Name: " & IPJ, "iLogic")

'------ end of iLogic ----------------------------------------------------------







******** Update (01/09/2013) ********
Here's some code to work with the project WorkSpace:

myWorkSpace = ThisApplication.FileLocations.Workspace
MessageBox.Show("WorkSpace Path:  " & myWorkSpace, "iLogic")



 And here's another snippet to work with project Workgroups:


Dim asNames() As String = {}
Dim asPaths() As String = {}
Dim iNumWorkgroups As Long
ThisApplication.FileLocations.Workgroups(iNumWorkgroups, asNames, asPaths)

If iNumWorkgroups = 0 Then
MessageBox.Show("No Workgroups are defined in the current project.", "iLogic")
Else
          i = 0
          Do until i = iNumWorkgroups
            wGName = asNames(i)
          wGPath = asPaths(i)
          MessageBox.Show("WorkGroup Name: " & wGName _
            & vblf & "WorkGroup Path: " & wGPath , "iLogic")
          i = i + 1
          Loop
End If