auto coding generator

auto coding generator

tecnico
Contributor Contributor
816 Views
10 Replies
Message 1 of 11

auto coding generator

tecnico
Contributor
Contributor
 

Good morning everyone,
I created this rule to automatically generate a code for our items.
The coding works well when creating a new file, but there is an issue when using the "create" or "save and replace" buttons within an assembly; I haven't found a way to make it work in these cases.
Does anyone have any ideas on how I could solve this?

Thank you.

 

Se  iProperties . Valore ( "Progetto" , "Numero parte" ) = ""  Quindi 
    Dim  risposta  As  MsgBoxResult 
    risposta = MsgBox ( "Manca il numero parte, vuoi assegnare una codifica?" & vbLf , vbYesNo , "Domanda" )
    
    If  risposta = vbYes  Then 
        ' Prosegui alla richiesta del tipo di articolo 
        Dim  articoloTipo  As  String 
        Do 
            articoloTipo = InputBox ( "Specifica il tipo di articolo (C, M, X, Y o digita 'P' per personalizzato):" , "Tipo di Articolo" )
             articoloTipo = UCase ( articoloTipo ) ' Converti l'input in maiuscolo per semplificare il controllo 
        Loop  While  articoloTipo <> "C"  E  articoloTipo <> "M"  E  articoloTipo <> "X"  E  articoloTipo <> "Y"  E  articoloTipo <> "P"
        
        If  articoloTipo = "P"  Then 
            articoloTipo = InputBox ( "Inserisci il tipo di articolo personalizzato:" , "Articolo personalizzato" )
             MsgBox ( "Hai scelto un tipo di articolo personalizzato: " & articoloTipo )
             ' Inserisci qui il codice per gestire un articolo personalizzato 
        Else 
            MsgBox ( "Hai scelto il tipo di articolo: " & articoloTipo )
            
            ' Apri il file excel delle codicifiche 
            GoExcel . Apri ( "K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx" , articoloTipo )
            
            ' Definisci la variabile per la colonna che vuoi controllare 
            Dim  Colonna  As  String = "A" 

            ' Definisci la variabile per memorizzare la riga della prima cella vuota 
            Dim  PrimaCellaVuota  As  Integer = 0

            ' Definisci una variabile separata per la riga corrente 
            Dim  RigaCorrente  As  Integer = 1

            ' Ciclo per trovare la prima cella vuota 
            Do 
                ' Verifica se la cella nella colonna specificata è vuota 
                If  String . IsNullOrEmpty ( GoExcel . CellValue ( Colonna & RigaCorrente )) Then 
                    ' Memorizza la riga della prima cella vuota 
                    PrimaCellaVuota = RigaCorrente 
                    ' Esci dal ciclo, poiché hai trovato la prima cella vuota 
                    Exit  Do 
                End  If

                ' Passa alla riga successiva 
                RigaCorrente = RigaCorrente + 1

                ' Continua il ciclo fino a trovare una cella vuota 
            Loop
            
            ' Correggi la variabile Riga in CELLA_ULTIMOVALORE 
            Dim  CELLA_ULTIMOVALORE  As  String = "A" & ( RigaCorrente - 1). ToString ()
             Dim  VAL_SEQ  As  String = GoExcel . CellValue ( "K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx" , articoloTipo , CELLA_ULTIMOVALORE )
            
			'INSERISCI IL VALORE IN PARTNUMBER 
			iProperties . Value ( "Project" , "Part Number" ) = articoloTipo & ( VAL_SEQ + 1)
			 MessageBox . Show ( iProperties . Value ( "Project" , "Part Number" ), "Title" )
			
			' scrivi il valore nel file excel 
			cellacod = Colonna & RigaCorrente 
			CELLA_utente = "B" & RigaCorrente 
			oNAME = ThisApplication . Opzioni generali . Nome utente 
			GoExcel . CellValue ( "K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx" , articoloTipo , cellacod ) = ( VAL_SEQ + 1)
			 GoExcel . CellValue ( "K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx" , articoloTipo , cellacod ) = ( VAL_SEQ + 1)
			 GoExcel . CellValue ( "K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx" , articoloTipo , CELLA_utente ) = oNOME 
			GoExcel . Salva 
			GoExcel . Chiudi 
	cambia il nome nel browser per suggerirlo poi nel salvataggio 
doc = ThisDoc . Documento

doc . DisplayName = iProperties . Value ( "Progetto" , "Numero parte" )

        End  If 
    Else 
        ' Codice per gestire il caso in cui il numero parte non sia vuoto 
    End  If 
End  If
0 Likes
817 Views
10 Replies
Replies (10)
Message 2 of 11

Michael.Navara
Advisor
Advisor

Where you get this code? It is from ChatGPT or another AI?

This code can't work! It can't be compiled!

0 Likes
Message 3 of 11

tecnico
Contributor
Contributor
 

I wrote part of the code myself and used AI for the rest, but I assure you it works.

0 Likes
Message 4 of 11

Michael.Navara
Advisor
Advisor

It doesn't work, because for example this methods are not defined on object GoExcel

...
GoExcel.Salva 
GoExcel.Chiudi 
...

 

0 Likes
Message 5 of 11

tecnico
Contributor
Contributor

I missed something in the translation, sorry.

 

If iProperties.Value("Project", "Part Number") = "" Then
    Dim response As MsgBoxResult
    response = MsgBox("Manca il numero parte, Vuoi assegnare una codifica?" & vbLf, vbYesNo, "Domanda")
    
    If response = vbYes Then
        ' Prosegui alla richiesta del tipo di articolo
        Dim articoloTipo As String
        Do
            articoloTipo = InputBox("Specifica il tipo di articolo (C, M, X, Y o digita 'P' per personalizzato):", "Tipo di Articolo")
            articoloTipo = UCase(articoloTipo) ' Converti l'input in maiuscolo per semplificare il controllo
        Loop While articoloTipo <> "C" And articoloTipo <> "M" And articoloTipo <> "X" And articoloTipo <> "Y" And articoloTipo <> "P"
        
        If articoloTipo = "P" Then
            articoloTipo = InputBox("Inserisci il tipo di articolo personalizzato:", "Articolo Personalizzato")
            MsgBox("Hai scelto un tipo di articolo personalizzato: " & articoloTipo)
            ' Inserisci qui il codice per gestire un articolo personalizzato
        Else
            MsgBox("Hai scelto il tipo di articolo: " & articoloTipo)
            
            ' Apri il file excel delle codifiche
            GoExcel.Open("K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx", articoloTipo)
            
            ' Definisci la variabile per la colonna che vuoi controllare
            Dim Colonna As String = "A" 

            ' Definisci la variabile per memorizzare la riga della prima cella vuota
            Dim PrimaCellaVuota As Integer = 0

            ' Definisci una variabile separata per la riga corrente
            Dim RigaCorrente As Integer = 1

            ' Ciclo per trovare la prima cella vuota
            Do
                ' Verifica se la cella nella colonna specificata è vuota
                If String.IsNullOrEmpty(GoExcel.CellValue(Colonna & RigaCorrente)) Then
                    ' Memorizza la riga della prima cella vuota
                    PrimaCellaVuota = RigaCorrente
                    ' Esci dal ciclo, poiché hai trovato la prima cella vuota
                    Exit Do
                End If

                ' Passa alla riga successiva
                RigaCorrente = RigaCorrente + 1

                ' Continua il ciclo fino a trovare una cella vuota
            Loop
            
            ' Correggi la variabile Riga in CELLA_ULTIMOVALORE
            Dim CELLA_ULTIMOVALORE As String = "A" & (RigaCorrente - 1).ToString()
            Dim VAL_SEQ As String = GoExcel.CellValue("K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx", articoloTipo, CELLA_ULTIMOVALORE)
            
			'INSERISCI IL VALORE IN PARTNUMBER
			iProperties.Value("Project", "Part Number") = articoloTipo & (VAL_SEQ + 1)
			MessageBox.Show(iProperties.Value("Project", "Part Number"), "Title")
			
			' scrivi il valore nel file excel
			cellacod = Colonna & RigaCorrente
			CELLA_utente = "B" & RigaCorrente
			oNAME = ThisApplication.GeneralOptions.UserName
			GoExcel.CellValue("K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx", articoloTipo, cellacod) = (VAL_SEQ + 1)
			GoExcel.CellValue("K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx", articoloTipo, cellacod) = (VAL_SEQ + 1)
			GoExcel.CellValue("K:\5_CODICIARTICOLI\ANAGRAFICA_UT.xlsx", articoloTipo, CELLA_utente) = oNAME
			GoExcel.Save
			GoExcel.Close
	'cambia il nome nel browser per suggerirlo poi nel salvataggio
doc = ThisDoc.Document

doc.DisplayName = iProperties.Value("Project", "Part Number")

        End If
    Else
        ' Codice per gestire il caso in cui il numero parte non sia vuoto
    End If
End If
0 Likes
Message 6 of 11

Michael.Navara
Advisor
Advisor

OK, now the code looks much better 😀

To your question. When the code works, it depends on when you want to run it. You can choose from default events in iLogic or use any other from the API.

In my opinion the relevant events in iLogic are NewDocument and BeforeSaveDocument.

MichaelNavara_0-1724915964480.png

NewDocument is fired when you exactly create new document form template

BeforeSaveDocument is fired when the document will be saved. Including the first save where document.FileSaveCounter = 0. At this time you can try to run your code.

 

Or you can look on another events available in API. There are lot of events fired for different reasons. But it requires to create an add-in, because in iLogic is hard to hoot to this events. Great tool for this purpose is EventWatcher from SDK

 

 

0 Likes
Message 7 of 11

tecnico
Contributor
Contributor

Currently, I use this, and it works when I press the 'new' button. In the save dialog, the code generated by the rule appears, which is fine. The problem arises when I use the 'create' function from within a *.iam file or the 'save and replace' function, which doesn't suggest the file name as it does in the first case.

tecnico_0-1724917266878.png

 

From here, it works because it suggests the code generated by the rule in the save dialog, as I set the newly generated code as the 'display.name'

 

tecnico_1-1724917385104.png

 

The problem is here:

tecnico_2-1724917633354.pngtecnico_3-1724917719477.png

 

 

0 Likes
Message 8 of 11

Michael.Navara
Advisor
Advisor

As I wrote before. You have to find an appropriate event(s), when you can run your code.

iLogic doesn't provide all possible events you can use in Inventor. 

 

Try to create minimalistic rules and put them to different events in iLogic UI (For example: Write single line of text to iLogic log window). Manually go thru the process and monitor when the rule is executed. If you find the right event, put here your code.

 

If you don't find anything useful, install Inventor SDK and use EventWatcher tool to monitor which events are fired during your process. Later on try to hook to this event and run your code from this event handler.

Possible events found in EventWatcher can be: FileUIEvents.OnPopulateFileMetadataFileUIEvents.OnFileSaveAsDialogApplicationEvents.OnSaveDocument

 

 

 

0 Likes
Message 9 of 11

tecnico
Contributor
Contributor

I used Event Watcher, and I believe this is the event that should trigger my rule. I’ve never used events or anything similar before; where could I find an example to use them?

 

tecnico_0-1725017805433.png

 

Thank you

0 Likes
Message 10 of 11

Michael.Navara
Advisor
Advisor

The best way for handling events which is not provided by iLogic is to create your own add-in. Below is the sample implementation of  add-in which handles OnPopulateFileMetadata event. But you need to look how to create add-in and this code sample is only for reference. Implementation for another events are very similar. 

 

  • In custom add-in it is hard to use iLogic code and objects (like GoExcel) and you need to rewrite your code.
  • Promising FileMetadata.Document property is usually empty (null, Nothing) and cant be used. Everything you have is just a file name. 
Class AddInServer
    Implements ApplicationAddInServer

    Private inventor As Inventor.Application
    Private fileUiEvents As FileUIEvents

    Public Sub Activate(addInSiteObject As ApplicationAddInSite, firstTime As Boolean) Implements ApplicationAddInServer.Activate
        inventor = addInSiteObject.Application

        fileUiEvents = inventor.FileUIEvents
        AddHandler fileUiEvents.OnPopulateFileMetadata, AddressOf FileUiEvents_OnPopulateFileMetadata
    End Sub


    Public Sub Deactivate() Implements ApplicationAddInServer.Deactivate

        RemoveHandler fileUiEvents.OnPopulateFileMetadata, AddressOf FileUiEvents_OnPopulateFileMetadata
        fileUiEvents = Nothing

        inventor = Nothing

        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub

    Public Sub ExecuteCommand(commandId As Integer) Implements ApplicationAddInServer.ExecuteCommand
        'Obsolete
    End Sub

    Public ReadOnly Property Automation As Object Implements ApplicationAddInServer.Automation
        Get
            Return Nothing
        End Get
    End Property

    Private Sub FileUiEvents_OnPopulateFileMetadata(fileMetadataObjects As ObjectsEnumerator, formulae As String, context As NameValueMap, ByRef handlingCode As HandlingCodeEnum)
        'PUT YOUR CODE HERE
    End Sub
End Class

 

 

General information about events are described on Microsoft .NET documentation 

0 Likes
Message 11 of 11

Maxim-CADman77
Advisor
Advisor

I see the topic is not new yet it is not marked as solved thus I believe nobody would object me post here..
Recently, I was asked whether I can customize filenaming on initial save of assembly and components created based on MultiBody Part.
After some investigation I became to think this can be done using that FileUIEvents.OnPopulateFileMetadata Event.

But for some reason I can't catch it with EventWatcher neither on Save nor On Component Create Command.
Could, please, somebody describe any action that initialize the Event?

 

UPDATE:
I do see the event in 2024 (my initial post was about 2026 and I do see Event there now ... probably there was some temporarily issue).

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes