2 Replies Latest reply on Jun 2, 2014 1:58 AM by m_ba

    Modification brought by script not saved

    m_ba

      Hi,

       

      I wrote a litte function to swap value of two attributes.

      VBA code is running fine. All sheets are opened and modified (modifications brought can be seen during code execution). Unfortunately only a part of these are saved. Such as a ‘save’ operation is missing.

      DxDesigner needs no ‘save’ due to the fact it’s working on a database (see p697 of DxDesigner Automation Reference Manual, EE 7.9.5 DocumentSave method).

      Could someone explain me what I’m doing wrong?

      Thanks

       

       

      Private Sub Swap_Properties()
          Dim My_Dx_App As Object
          Dim My_Dx_Prj As String
          Dim My_Block As Variant

          Dim My_Block_Sheets As Variant
          Dim My_Block_Sheet As Variant
          Dim My_WorkSheet As Object
         
          Dim My_Components As Object
          Dim My_Component As ViewDraw.Component
          Dim My_Component_Attribute As ViewDraw.Attribute

          Dim My_Comp_PartNumber_Value As String
          Dim My_Comp_PartNumber_Visible As Integer
          Dim My_Comp_PartLabel_Value As String
          Dim My_Comp_PartLabel_Visible As Integer

          Dim i As Integer

       

          i = 0

          My_Comp_PartNumber_Value = ""
          My_Comp_PartNumber_Visible = 0
          My_Comp_PartLabel_Value = ""
          My_Comp_PartLabel_Visible = 0


          Set My_Dx_App = GetObject(, "ViewDraw.Application") 'DxDesigner need to be open!
          If My_Dx_App Is Nothing Then
              MsgBox "Could not connect to DxDesigner Apllication!", vbCritical
          Else
              My_Dx_Prj = My_Dx_App.GetActiveDesign 'Check if there's an open project
              If My_Dx_Prj = "" Then
                  MsgBox "No open project found!", vbExclamation
              Else
                  For Each My_Block In My_Dx_App.SchematicSheetDocuments.GetAvailableSchematics
                      
                      Set My_Block_Sheets = My_Dx_App.SchematicSheetDocuments.GetAvailableSheets(My_Block)
                     
                      For Each My_Block_Sheet In My_Block_Sheets
                          Set My_WorkSheet = My_Dx_App.SchematicSheetDocuments.Open(My_Block, My_Block_Sheet)

                          My_WorkSheet.Activate
                          Set My_Components = My_Dx_App.Query(VDM_COMP, VD_ALL)
                          If My_Components.Count > 0 Then
                              For Each My_Component In My_Components
             
                                  Set My_Component_Attribute = My_Component.FindAttribute("Part Number")
                                  If Not My_Component_Attribute Is Nothing Then
                                      My_Comp_PartNumber_Value = My_Component_Attribute.Value
                                      My_Comp_PartNumber_Visible = My_Component_Attribute.Visible
                                  End If
             
                                  Set My_Component_Attribute = My_Component.FindAttribute("Part Label")
                                  If Not My_Component_Attribute Is Nothing Then
                                      My_Comp_PartLabel_Value = My_Component_Attribute.Value
                                      My_Comp_PartLabel_Visible = My_Component_Attribute.Visible
                                  End If


                                  If Not (My_Comp_PartNumber_Value = "" Or My_Comp_PartLabel_Value = "") Then
                                 
                                  '*** Swap the two attributes ***

                                      Set My_Component_Attribute = My_Component.FindAttribute("Part Number")
                                      If Not My_Component_Attribute Is Nothing Then
                                          My_Component_Attribute.Value = My_Comp_PartLabel_Value
                                          My_Component_Attribute.Visible = My_Comp_PartLabel_Visible
                                      End If
             
                                      Set My_Component_Attribute = My_Component.FindAttribute("Part Label")
                                      If Not My_Component_Attribute Is Nothing Then
                                          My_Component_Attribute.Value = My_Comp_PartNumber_Value
                                          My_Component_Attribute.Visible = My_Comp_PartNumber_Visible
                                      End If
             
                                  '*** End Swap the two attributes ***
                                  End If
             
                                  i = i + 1
                                  My_Comp_PartNumber_Value = ""
                                  My_Comp_PartNumber_Visible = 0
                                  My_Comp_PartLabel_Value = ""
                                  My_Comp_PartLabel_Visible = 0
             
                              Next
                          End If
                          My_WorkSheet.Close True, My_Block_Sheet
                          'My_Dx_App.SchematicSheetDocuments.Close 'close all schematic and not only the selected one!!
                      Next
                  Next
              End If
          End If


      MyEndSub:
          Set My_Component_Attribute = Nothing
          Set My_Component = Nothing
          Set My_Components = Nothing
          Set My_WorkSheet = Nothing
          Set My_Block_Sheet = Nothing
          Set My_Block_Sheets = Nothing
          Set My_Block = Nothing
          'Set My_Dx_Prj = Nothing
          Set My_Dx_App = Nothing
          Debug.Print "Finished " & Now()
      End Sub

        • 1. Re: Modification brought by script not saved
          Patrick.Cashman

          Part Number and Part Label cannot be changed in the schematic. It's not an automation problem, it's a system and database integrity issue that is that way intentionally. The fix is to make the library part correct and then use the correct library part.

          • 2. Re: Modification brought by script not saved
            m_ba

            Please find code update (problem is solved)

            If library and schematic match (see partnumber value) no corruption is observed.

            Regards

             

             

            Option Explicit
            'To run in VBA: ViewDraw need to be added to Tools > References list

            #Const DebugMode = False
            #Const Swap = True

             

            Private Enum MyEnumOutput
              DxOutputTab
              DxMyScriptTab
              VBAImmediate
              vbMsgBox
            End Enum

             

            Private My_Dx_App As ViewDraw.Application 'Object - DxDesigner Application (ViewDraw)

            Private Sub Swap_Properties()
            '**************************************************************************************************
            '*** Swap "Part Number" and "Part Label" attributes value on current open design on DxDesigner  ***
            '*** Before running script open project into DxDesigner                                         ***
            '**************************************************************************************************
                Dim My_Dx_Prj As String

                Dim My_Documents As Variant 'SchematicSheetDocuments Collection
                Dim My_SchematicSheetDocs As ViewDraw.StringList 'SchematicSheetDocuments Collection

                Dim My_SchematicSheetDoc As Variant 'ViewDraw.SchematicSheetDocument
                Dim My_WorkDoc As Object 'ViewDraw.SchematicSheetDocument

                Dim My_Components As Object 'ViewDraw.Components
                Dim My_Component As ViewDraw.Component
                Dim My_Component_Attribute As ViewDraw.Attribute

                '*** Select Output ***
                Dim My_Output As MyEnumOutput
                My_Output = MyEnumOutput.VBAImmediate '.DxMyScriptTab 'Select location of the output information
                '*** End Select Output ***

                Dim My_Comp_RefDes As String
                Dim My_Comp_PartNumber_Value As String
                Dim My_Comp_PartNumber_Visible As Integer
                Dim My_Comp_PartLabel_Value As String
                Dim My_Comp_PartLabel_Visible As Integer

                Dim i As Integer

             

                #If DebugMode Then
                    Dim sFile As String
                    Dim lFile As Long
                    Dim sFile_Line As String

                    sFile = "d:\testfile.txt"
                    lFile = FreeFile

                    'Open sFile For Append As lFile 'Append data to file
                    Open sFile For Output As lFile 'Overwrite all previous data in file
                #End If


                i = 0

                My_Comp_RefDes = ""
                My_Comp_PartNumber_Value = ""
                My_Comp_PartNumber_Visible = 0
                My_Comp_PartLabel_Value = ""
                My_Comp_PartLabel_Visible = 0


                Set My_Dx_App = GetObject(, "ViewDraw.Application") 'DxDesigner needs to be open!
                If My_Dx_App Is Nothing Then
                    My_Output_Screen vbMsgBox, "Could not connect to DxDesigner Apllication!"
                Else
                    My_Dx_Prj = My_Dx_App.GetActiveDesign 'Check if there's an open project
                    My_Output_Screen My_Output, "Starting script on : " & Now()
                    If My_Dx_Prj = "" Then
                        My_Output_Screen My_Output, "No open project found!"
                    Else
                        My_Output_Screen My_Output, "Script is running on project """ & My_Dx_Prj & """"
                        My_Dx_App.SchematicSheetDocuments.Close 'close all schematics / Mandatory with My_Dx_App.Query call (see below)

                        For Each My_Documents In My_Dx_App.SchematicSheetDocuments.GetAvailableSchematics 'Documents is an automation collection that represents the set of DxDesigner schematic documents
                            Set My_SchematicSheetDocs = My_Dx_App.SchematicSheetDocuments.GetAvailableSheets(My_Documents) 'Return the Sheets contained in the Block
                            For Each My_SchematicSheetDoc In My_SchematicSheetDocs
                                Set My_WorkDoc = My_Dx_App.SchematicSheetDocuments.Open(My_Documents, My_SchematicSheetDoc)
                                My_WorkDoc.Activate 'Mandatrory with My_Dx_App.ActiveView.Query call (see below)
                               
                                'Set My_Components = My_Dx_App.Query(VDM_COMP, VD_ALL) 'Components collection from all open design
                                Set My_Components = My_Dx_App.ActiveView.Query(VDM_COMP, VD_ALL) 'Components collection from only Active View
                                If My_Components.Count > 0 Then
                                    For Each My_Component In My_Components
                                        '*** Get Component Attributes ***
                                        Set My_Component_Attribute = My_Component.FindAttribute("Ref Designator") 'This finds the attribute itself
                                        If Not My_Component_Attribute Is Nothing Then
                                            My_Comp_RefDes = My_Component_Attribute.Value
                                        End If

                                        Set My_Component_Attribute = My_Component.FindAttribute("Part Number") 'This finds the attribute itself
                                        If Not My_Component_Attribute Is Nothing Then
                                            My_Comp_PartNumber_Value = My_Component_Attribute.Value
                                            My_Comp_PartNumber_Visible = My_Component_Attribute.Visible
                                        End If

                                        Set My_Component_Attribute = My_Component.FindAttribute("Part Label") 'This finds the attribute itself
                                        If Not My_Component_Attribute Is Nothing Then
                                            My_Comp_PartLabel_Value = My_Component_Attribute.Value
                                            My_Comp_PartLabel_Visible = My_Component_Attribute.Visible
                                        End If

                                        If Not (My_Comp_RefDes = "" Or My_Comp_PartNumber_Value = "" Or My_Comp_PartLabel_Value = "") Then
                                            '*** Swap the two attributes ***
                                            #If Swap Then
                                                Set My_Component_Attribute = My_Component.FindAttribute("Part Number") 'Finds the attribute
                                                If Not My_Component_Attribute Is Nothing Then
                                                    My_Component_Attribute.Value = My_Comp_PartLabel_Value
                                                    My_Component_Attribute.Visible = My_Comp_PartLabel_Visible
                                                End If

                                                Set My_Component_Attribute = My_Component.FindAttribute("Part Label") 'Finds the attribute
                                                If Not My_Component_Attribute Is Nothing Then
                                                    My_Component_Attribute.Value = My_Comp_PartNumber_Value
                                                    My_Component_Attribute.Visible = My_Comp_PartNumber_Visible
                                                End If

                                                #If DebugMode Then
                                                    '*** Swap vérification ***
                                                    sFile_Line = i & vbTab & My_Component.UID & vbTab & My_WorkDoc.Name & vbTab & My_Comp_RefDes & vbTab & My_Comp_PartNumber_Value & vbTab & My_Comp_PartLabel_Value
                                                    My_Output_Screen My_Output, sFile_Line
                                                    Print #lFile, sFile_Line
                                                    '*** End Swap vérification ***
                                                #End If
                                            #End If
                                            '*** End Swap the two attributes ***
                                        End If

                                        i = i + 1
                                        My_Comp_RefDes = ""
                                        My_Comp_PartNumber_Value = ""
                                        My_Comp_PartNumber_Visible = 0
                                        My_Comp_PartLabel_Value = ""
                                        My_Comp_PartLabel_Visible = 0
                                    Next
                                End If
                                My_WorkDoc.Close True, My_SchematicSheetDoc 'Mandatory with My_Dx_App.Query call (see above)
                            Next
                        Next
                        'My_Dx_App.SchematicSheetDocuments.Close 'close all schematics and not only the selected one!!
                    End If
                End If


            MyEndSub:
                #If DebugMode Then
                    Close lFile
                #End If

                Set My_Component_Attribute = Nothing
                Set My_Component = Nothing
                Set My_Components = Nothing
                Set My_WorkDoc = Nothing
                Set My_SchematicSheetDoc = Nothing
                Set My_SchematicSheetDocs = Nothing
                Set My_Documents = Nothing
                My_Output_Screen My_Output, "Ending script on : " & Now()

                Terminate
            End Sub

             

            Private Sub Terminate()
                Set My_Dx_App = Nothing
            End Sub

             

            Private Sub My_Output_Screen(OutputWindow As MyEnumOutput, OutputString As String)
                Select Case OutputWindow
                    Case MyEnumOutput.DxOutputTab:
                        Call My_Dx_App.AppendOutput("Output", OutputString)
                    Case MyEnumOutput.DxMyScriptTab:
                        My_Dx_App.AppendOutput "My Script", OutputString
                    Case MyEnumOutput.VBAImmediate:
                        Debug.Print OutputString
                    Case Else
                        MsgBox OutputString
                End Select
            End Sub