1 Reply Latest reply on Apr 17, 2015 2:28 PM by Bruce Perry

    Need help with checking if a view exist in an assembly or a model; if not to create it?

    Bruce Perry

      Need Help trying to loop through the assembly views (VBA) to find a view named "Door" If "Door" doesen't exist create it. My Problems is not creating the view or getting the view names my problem is getting it together; seem you have to setup two types of arrays with one inside the other; first one gets a value and it's a question of getting the right order with the if statement; if exist exit; if not create it????

        • Re: Need help with checking if a view exist in an assembly or a model; if not to create it?
          Bruce Perry

          I did not get any reply, but I did find my answer in case anyone is interested.

          '****************************

          'Author: Bruce A. Perry 4/17/2015

          'Scope: Get all the named views and check and see if

          'a specified named view (door) exist as as named view

          'if the view door does not exist; use a standard view

          '(ShowNamedView2) to start then manipulate it to get

          'the view you want then name it door.

          'If you re run this code it will not create door again

          'but, will skip to a: and exit.

          'Prerequsite: Model or Assembly has to be active; this

          'code was written to get the concept down, and to be later

          'include into large code.

          'Will error if no active document or drawing is active.

          '**************************

          Sub main()

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim vModelViewNames As Variant

          Dim cModelViewNames As Variant

          Dim i As Variant

           

              Set swApp = CreateObject("SldWorks.Application")

              Set swModel = swApp.ActiveDoc

          vModelViewNames = swModel.GetModelViewNames 'Get view names

           

          '*******Ten Standard Views********

          '*Normal To'*Front'*Back'*Left'*Right

          '*Top'*Bottom'*Isometric'*Trimetric'*Dimetric

          '****************

           

          For i = 0 To UBound(vModelViewNames) 'Go from the

          'begining to the end of the names

           

          'Note: The name door is case sensitive

          If (vModelViewNames(i) = "door") Then GoTo a: 'If door is

          'encountered skip over to a: the end! Else if not GoTo

          'Next i or Next Named view

           

          Next i

           

          'If door is not found in then got to the Top view

          'Turn the view 180 degrees and create door view

              swModel.ShowNamedView2 "*Top", 5

              swModel.ViewZoomtofit2

              swModel.ViewRotYPlusNinety

              swModel.ViewRotYPlusNinety

              swModel.ViewZoomtofit2

              swModel.NameView ("door")

           

          a: 'door is found so stop here and quit

           

          End Sub