writing back iproperty, drawing to part, convert iLogic to vba (now VB.net visual Studio)

writing back iproperty, drawing to part, convert iLogic to vba (now VB.net visual Studio)

andrew_canfield
Collaborator Collaborator
720 Views
4 Replies
Message 1 of 5

writing back iproperty, drawing to part, convert iLogic to vba (now VB.net visual Studio)

andrew_canfield
Collaborator
Collaborator

 

Hello,

I can open & replace drawing views using vba, but I also like to write the drawing number & file path of the drawing back to the part - already working in iLogic for individual drawings but the vba is for a batch of drawings.

 

 

		Dim docFName As String
	docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
	'MessageBox.Show("docFName: " +docFName, "Debug")
	
	' the file in the drawing is docFName, copy the PDM drawing number to it
	
	iProperties.Value(docFName, "Custom", "PDM_Dwg_No") = DwgNo
	'iProperties.Value(docFName,"Project", "Part Number")=  DwgNo
		fp = ThisDoc.Path
	iProperties.Value(docFName, "Custom", "PDM_Dwg_No_Path") = fp
	''
	iProperties.Value(docFName, "Project", "Part Number") = DwgNo
	iProperties.Value(docFName, "CUSTOM", "Number") = DwgNo
	iProperties.Value(docFName, "CUSTOM", "ID") = DwgNo

 

This is the vba - the spreadsheet has  filepaths to a list of drawings in column 1 & list of models in column 2.

The drawings are created with title blocks set up & project notes & a dummy part with 3 views & isometric.

Run the vba & the dummy part is swapped for a part on the list.

Need to add code to save & close, but need to write back to the part iproperties first.

 

Public Sub GetExcelData()
    Dim excelApp As Excel.Application
    ' Try to connect to a running instance of Excel.
    On Error Resume Next
    Set excelApp = GetObject(, "Excel.Application")
    'MsgBox "ac1"
    If Err Then
        Err.Clear
        
        ' Couldn't connect so start Excel.  It's started invisibly.
        Set excelApp = CreateObject("Excel.Application")
        
        If Err Then
            MsgBox "Cannot access excel."
            Exit Sub
        End If
    End If
    
    ' You can make it visible if you want.  This is especially
    ' helpful when debugging.
    excelApp.Visible = True
    
    ' Open the spreadsheet.
    Dim wb As Workbook
    Set wb = excelApp.Workbooks.Open("C:\Users\a.canfield\Desktop\Batch Export\DrawingNumber.xlsx")
    If Err Then
        MsgBox "Unable to open the Excel document."
        Exit Sub
    End If
    'MsgBox "ac2"
    ' Access a certain sheet.
    Dim ws As WorkSheet
    Set ws = wb.Worksheets.Item("Sheet1")
    If Err Then
        MsgBox "Unable to get the worksheet."
        Exit Sub
    End If
    
    ' Read some values from the sheet.
    Dim row As Integer
    Dim row2 As Integer
    Dim col As Integer
        
    row2 = Application.InputBox("Please enter number of rows in DrawingNumber.xlsx")
   
    For row = 1 To row2
    
    
        col = 2
            'Debug.Print "Row: " & row & ", Col: " & col & " = " & ws.Cells(row, col)
            DwgName = ws.Cells(row, 1)
            PartName = ws.Cells(row, 2)
    
Set oDoc = ThisApplication.Documents.Open(DwgName)
oDoc.File.ReferencedFileDescriptors(1).ReplaceReference (PartName)
''
    ' Get the PropertySets object.
    Dim oPropSets As PropertySets
    Set oPropSets = oDoc.PropertySets

    ' Get the design tracking property set.
    Dim oPropSet As PropertySet
    Set oPropSet = oPropSets.Item("Design Tracking Properties")

    ' Get the part number iProperty.
    Dim oPartNumiProp As Property
    Set oPartNumiProp = oPropSet.Item("Part Number")
    
   

        'Dim docFName As String
    'docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
    'MessageBox.Show("docFName: " +docFName, "Debug")
    'MsgBox DwgName
    MsgBox PartName
     MsgBox oPartNumiProp.Value
    ' the file in the drawing is docFName, copy the PDM drawing number to it
    
'    Dim oPrt As String
'
'oPrt = ThisApplication.Documents.ItemByName(PartName)
'
'    ' Get the active document.
'    'Dim oDoc As Document
'    'Set oPrt = ThisApplication.ActiveDocument
'
'    ' Get the PropertySets object.
'    Dim oPropSetsPrt As PropertySets
'    Set oPropSetsPrt = oDoc.PropertySets
'
'    ' Get the design tracking property set.
'    Dim oPropSetPrt As PropertySet
'    Set oPropSetPrt = oPropSets.Item("Design Tracking Properties")
'
'    ' Get the part number iProperty.
'    Dim oPartNumiProp As Property
'    Set oPartNumiProp = oPropSetPrt.Item("Part Number")
'
'    ' Set the part number.
'    oPartNumiProp.Value = "SamplePart001"

        

Next

End Sub

 

 

Regards

 

Andrew

0 Likes
Accepted solutions (1)
721 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @andrew_canfield.  I'm not sure I fully understand your whole process, but here is my interpretation of what you are looking for.  At first I just had it directly copying drawing iProperty values over to the same property on the part side, because I was not sure what 'DwgNo' variable represented in your iLogic snippet above.  Then I looked back at your other post and saw that it was supposed to be the drawing file's name, without path, and without file extension.  Then I saw that you were using the iLogic shortcut snippet for every iProperty setting, which led me to wander whether or not the part document would always already have all of those custom iProperties or not.  I know that snippet will automatically create the custom iProperty if it is not found, when you are setting a value with it.  But I did not include code to check if they exist, or to create them if not found.  I just assumed you already had all that stuff set-up ahead of time.

Public Sub GetExcelData()
    Dim excelApp As Excel.Application
    ' Try to connect to a running instance of Excel.
    On Error Resume Next
    Set excelApp = GetObject(, "Excel.Application")
    'MsgBox "ac1"
    If Err Then
        Err.Clear
        ' Couldn't connect so start Excel.  It's started invisibly.
        Set excelApp = CreateObject("Excel.Application")
        If Err Then
            MsgBox "Cannot access excel."
            Exit Sub
        End If
    End If
    
    ' You can make it visible if you want.  This is especially
    ' helpful when debugging.
    excelApp.Visible = True
    
    ' Open the spreadsheet.
    Dim wb As Workbook
    Set wb = excelApp.Workbooks.Open("C:\Users\a.canfield\Desktop\Batch Export\DrawingNumber.xlsx")
    If Err Then
        MsgBox "Unable to open the Excel document."
        Exit Sub
    End If
    'MsgBox "ac2"
    ' Access a certain sheet.
    Dim ws As WorkSheet
    Set ws = wb.Worksheets.Item("Sheet1")
    If Err Then
        MsgBox "Unable to get the worksheet."
        Exit Sub
    End If
    
    ' Read some values from the sheet.
    Dim row As Integer
    Dim row2 As Integer
    Dim col As Integer
    row2 = Application.InputBox("Please enter number of rows in DrawingNumber.xlsx")
    For row = 1 To row2
        Dim DwgName As String
        DwgName = ws.Cells(row, 1).Value
        Dim PartName As String
        PartName = ws.Cells(row, 2).Value
        
        Dim oDDoc As DrawingDocument
        Set oDDoc = ThisApplication.Documents.Open(DwgName)
        Dim oDrawingPath As String
        oDrawingPath = Left(oDDoc.FullFileName, InStrRev(oDDoc.FullFileName, "\"))
        Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
        Dim oDrawingName As String
        oDrawingName = FSO.GetBaseName(DwgName)
            
        Dim oPDoc As PartDocument
        Set oPDoc = ThisApplication.Documents.Open(PartName)
        
        oDDoc.File.ReferencedFileDescriptors(1).ReplaceReference (PartName)
        
        oPDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = oDrawingName
        
        'may need to create all of these 'Custom' iProperties, instead of just set their values
        Dim oCProps As PropertySet
        Set oCProps = oPDoc.PropertySets.Item("Inventor User Defined Properties")
        
        oCProps.Item("PDM_Dwg_No").Value = oDrawingName
        oCProps.Item("PDM_Dwg_No_Path").Value = oDrawingPath
        oCProps.Item("Number").Value = oDrawingName
        oCProps.Item("ID").Value = oDrawingName
    Next
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

andrew_canfield
Collaborator
Collaborator

Many Thanks.

The data management system has a numbering system in place - it's sequential, right click to generate a new drawing & can also be slow, where a dialog box requires details to be entered. (these details can be common, Building number, System Group etc - so a bit of repetition).

When a part is created, there's no way of knowing which drawing it will end up on - so after it is placed on a drawing, the drawing details are written back to the part. A custom iproperty is used in place of the part number (PDM_Dwg_No) which is used in the Parts List on the drawing - the drawing number becomes the Part Number.

Hopefully now, a batch of drawings can be created & running your code will place a list of models onto the drawing.

Will test some more.

Regards

 

Andrew

 

0 Likes
Message 4 of 5

andrew_canfield
Collaborator
Collaborator

A couple of references:

https://www.autodesk.com/autodesk-university/class/Creating-Add-Ins-Inventor-2018#video

https://www.autodesk.com/autodesk-university/class/iLogic-and-Inventor-API-2016

& a bit of youtube - has lead me here:

excelReference.PNG

One day I hope to be able to use Visual Studio..

 

Regards

 

Andrew

0 Likes