7 Replies Latest reply on Mar 26, 2019 1:04 AM by peter_festesen

    How to add a property to the parts in Central Library

    olsmir

      Hello.

       

      Does anybody know how to add a property to the parts in Central Library?

      I need to automatically add the "Attachment" property to each part in the Central Library where this property is missing.

      Based on the library_editors_auto.pdf, I have created the following script:

       

      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

      Option Explicit

      Dim app,lib
      Dim pdbEditor,pdbDB,i,j,k,m,flag
      Dim PartitionsColl,PartsColl,PropertiesColl
      Dim partition,part,propty,proptynew

      Set app = GetObject (,"LibraryManager.Application")
      Scripting.AddTypeLibrary ("LibraryManager.Application")
      Set lib = app.ActiveLibrary
      Call AddAttr


      Sub AddAttr()

      Set pdbEditor = CreateObject("MGCPCBLibraries.PartsEditorDlg")
      Scripting.AddTypeLibrary ("MGCPCBLibraries.PartsEditorDlg")
      Set pdbDB = pdbEditor.OpenDatabaseEx(lib.FullName, False)
      pdbEditor.Visible = False

      Set proptynew = Nothing

      Set PartitionsColl = pdbDB.Partitions
      For Each partition In PartitionsColl

          If (partition.Name = "Amplifier_BaseC") Then

          Set PartsColl = partition.Parts
          For Each part In PartsColl
              flag = 0
              Set PropertiesColl = part.Properties
              For Each propty In PropertiesColl
                  If (propty.Name = "Attachment") Then flag = 1
              Next

              If (flag = 0) Then proptynew = part.PutPropertyEx("Attachment","some path")

          Next

          End if

      Next

      pdbEditor.SaveActiveDatabase
      pdbEditor.Quit
      MsgBox "End", 0, ""

      End Sub

       

      ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

       

      I have opened my CL in Library Manager, and invoked the above script. However, it has failed stating that "proptynew" is not supported in line 36.

      Line 36 is: If (flag = 0) Then proptynew = part.PutPropertyEx("Attachment","some path")

       

      Can anybody tell me what is wrong and how to correct my script?

       

      Oleg

        • 1. Re: How to add a property to the parts in Central Library
          Mike_Walsh

          Off the top of my head, I think the syntax of this line is wrong:

           

          If (flag = 0) Then proptynew = part.PutPropertyEx("Attachment","some path")

           

          I think it should be somehting like:

           

          If (flag = 0) Then

              part.PutPropertyEx("Attachment","some path")

          End If

           

           

          You may also have to "commit" the part using part.Commit but I am not entirely positive about that.

           

          Mike

          • 2. Re: How to add a property to the parts in Central Library
            olsmir

            No, such synrax is correct. We can miss "End IF" since there is only one line after "Then".

            • 3. Re: How to add a property to the parts in Central Library
              john_dube

              Hi Oleg,

               

              The PutPropertyEx method returns an object (IMGCPDBProperty), so you need to use the Set statement:

               

                    If (flag = 0) Then Set proptynew = part.PutPropertyEx("Attachment","some path")

               

              John

              • 4. Re: How to add a property to the parts in Central Library
                olsmir

                Hi John,

                 

                Thank you very much. Yes, it does work. I know that "proptynew" is an object, so I defined it earlier as "Set proptynew = Nothing". I was wrong with it.

                 

                Oleg

                • 5. Re: How to add a property to the parts in Central Library
                  peter_festesen

                  Oleg - would you mind posting the final script ?

                  • 6. Re: How to add a property to the parts in Central Library
                    olsmir

                    'Note: <reference file with the full path> means that in fact I read this from pre-defined listfile.

                    'This listfile may have the following syntax:

                    '<part number1> <reference file1 with the full path>

                    '<part number2> <reference file2 with the full path>

                    '.....................................................................

                     

                    Option Explicit

                    Dim app,lib
                    Dim fso,logfile
                    Dim pdbEditor,pdbDB
                    Dim PartitionsColl,PartsColl,PropertiesColl
                    Dim partition,part,propty,proptynew
                    Dim flag

                    Set app = GetObject (,"LibraryManager.Application")
                    Scripting.AddTypeLibrary ("LibraryManager.Application")
                    Set lib = app.ActiveLibrary
                    Call AddAttr

                    Sub AddAttr()

                    Set fso = CreateObject ("Scripting.FileSystemObject")
                    Set logfile = fso.CreateTextFile (lib.Path & "\LogFiles\AddAttachment.txt", True)

                    Set pdbEditor = CreateObject("MGCPCBLibraries.PartsEditorDlg")
                    Scripting.AddTypeLibrary ("MGCPCBLibraries.PartsEditorDlg")
                    Set pdbDB = pdbEditor.OpenDatabaseEx(lib.FullName, False)
                    pdbEditor.Visible = False

                    Set PartitionsColl = pdbDB.Partitions
                    For Each partition In PartitionsColl
                        Set PartsColl = partition.Parts
                        For Each part In PartsColl
                            flag = 0
                            Set PropertiesColl = part.Properties
                            For Each propty In PropertiesColl
                                If (propty.Name = "Attachment") Then flag = 1
                            Next

                            If (flag = 0) Then
                               Set proptynew = part.PutPropertyEx("Attachment", <reference file with the full path>)
                               logfile.WriteLine (partition.Name & ": " & part.Number & " - Attachment = " & <reference file with the full path>)
                            End If

                        Next
                    Next

                    pdbEditor.SaveActiveDatabase
                    pdbEditor.Quit
                    logfile.Close
                    MsgBox "Done, see also AddAttachment.txt", 0, ""

                    End Sub

                    • 7. Re: How to add a property to the parts in Central Library
                      peter_festesen

                      Thanks a bunch, olsmir !!!!

                      For everybody's info, I ran into a problem yesterday that took me a little time to figure out what the problem really was (even though the error message was clear in the MGC BCD CMS shell).

                       

                      Source:'PDBEditor'

                      Line:49  Char:6

                      Error:0x80040221  'Property is not defined.'

                       

                      I had not thought about the fact that the property you are adding, MUST be defined in the Property Definition Editor beforehand.

                       

                      Once that was overcome, I was able to solve the issue. I will post/upload my example to another thread (Help needed Script fill properties, to be updated later today), since the prerequisite was a little different from the one in this thread.

                      /Peter