1 Reply Latest reply on Feb 28, 2015 3:30 AM by Keith Rice

    macro to delete all lighting in a part

    Jeff Pruet


      i can't figure out how to delete all the custom lights from a part.

      i'm a bit vb illiterate.  maybe one of you experts could enlighten me with a nice macro?

        • Re: macro to delete all lighting in a part
          Keith Rice



          This will delete all direction lights from a part:


          Sub main()
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Do While swModel.GetLightSourceCount > 1
                  swModel.DeleteLightSource 1
          End Sub


          The light source index starts at 1. The Ambient light, which is undeletable, is index 0. Whenever you delete a directional light, any remaining directional lights drop down in the index, so you can't crawl forward with a For loop and instead must use a Do Loop like above.


          If, for some reason, a directional light can't be deleted, then this code will result in an infinite loop. For safety reasons you might include a counter that eventually triggers leaving the loop if the counter hits a certain value.



          SolidWorks API Tutorials