' Autore: Rossano Praderi
' Creato il: 21/06/2025
Sub main
Try
' PartDocument di riferimento
Dim oDoc As Inventor.PartDocument = Nothing
If (ThisDoc.Document.DocumentType.Equals(Inventor.DocumentTypeEnum.kDrawingDocumentObject))
' se il documento attivo è un DrawingDocument assegna ad oDoc il primo documento di riferimento
oDoc = ThisDoc.Document.ReferencedDocuments.Item(1)
ElseIf (ThisDoc.Document.DocumentType.Equals(Inventor.DocumentTypeEnum.kPartDocumentObject))
' se il documento attivo è un PartDocument assegna ad oDoc il documento attivo
oDoc = ThisDoc.Document
End If
' se oDoc è Nothing prosegue senza fare nulla
If Not oDoc Is Nothing
Dim shm As SheetMetalComponentDefinition = oDoc.ComponentDefinition
' Propertyset "Custom"
Dim pSet As Inventor.PropertySet = oDoc.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
' numero pezzi ***** DUE OPZIONI DISPONIBILI *****
' se il parametro "v" non è impostato, viene visualizzata la richiesta di inserire il numero pezzi solo quando l'IProperty non esiste e deve essere creata
pz = CreateIfNotExists_Int(pSet, "PEZZI")
' se il parametro "v" viene impostato a -1, viene sempre chiesto di impostare il numero pezzi
'pz = CreateIfNotExists_Int(pSet, "PEZZI", -1)
' extents_length
CreateIfNotExists(pSet, "extents_length", ToMeters(shm.FlatPattern.Length))
' extents_width
CreateIfNotExists(pSet, "extents_width", ToMeters(shm.FlatPattern.Width))
' area
Area = ToMeters(shm.FlatPattern.Length * shm.FlatPattern.Width)
CreateIfNotExists(pSet, "extents_area", Area)
' area totale - imposta ad 1 il numero pezzi se il valore è inferiore ad 1
CreateIfNotExists(pSet, "extents_area_total", Area * Math.Max(1, pz))
End If
Catch
End Try
End Sub
''' <summary>
''' Assegna il valore all'IPropertiy e se non esiste la crea(versione con InputBox per valore numerico intero)
''' </summary>
''' <param name="pSet">PropertySet di riferimento</param>
''' <param name="pName">Nome IProperty di riferimento</param>
''' <param name="v">Valore da assegnare all'IProperty di riferimento</param>
''' <returns>Restituisce un valore numerico intero</returns>
Private Function CreateIfNotExists_Int(ByRef pSet As Inventor.PropertySet, pName As String, Optional v As Integer = 0) As Integer
Dim prop As Inventor.Property
Try
prop = pSet.Item(pName)
prop.Value = v
Catch
v = -1
prop = pSet.Add(v, pName)
End Try
if v = -1
v = CInt(InputBox("Immettere un valore numerico", "Valore IProperty " & pName))
prop.Value=v
end if
CreateIfNotExists_Int = v
End Function
''' <summary>
''' Assegna il valore all'IPropertiy e se non esiste la crea(versione senza InputBox)
''' </summary>
''' <param name="pSet">PropertySet di riferimento</param>
''' <param name="pName">Nome IProperty di riferimento</param>
''' <param name="v">Valore da assegnare all'IProperty di riferimento</param>
Private Sub CreateIfNotExists(ByRef pSet As Inventor.PropertySet, pName As String, v As Object)
Dim prop As Inventor.Property
Try
prop = pSet.Item(pName)
prop.Value = v
Catch
prop = pSet.Add(v, pName)
End Try
End Sub
''' <summary>
''' Funzione per convertire un valore numerico double(con vigola mobile) da millimetri a metri
''' </summary>
''' <param name="v">Valore numerico con virgola da convertire</param>
''' <param name="nd">Numero di decimali d'arrotondamento, numero decimali di default è 3</param>
''' <returns></returns>
Private Function ToMeters(v As Double, Optional nd As Integer = 3) As Double
ToMeters = Math.Round(ThisApplication.UnitsOfMeasure.ConvertUnits(v, UnitsTypeEnum.kMillimeterLengthUnits, UnitsTypeEnum.kMeterLengthUnits), nd)
End Function