While Loop problems?

Hello everyone,

I have a while loop that im using to prompt the user to select a face so that a sketch may be started on it.

everything works fine (Im able to spin the model around and make selections) when testing it in the vb editor but when I run it from within soldiworks using the "Run Macro" button I cannot spin the model around nor make selections. essentially SW locks up and the only way out is to end the process.

is there something wrong with the loop im using or the way im connecting to solidworks?

Option Explicit

Dim swApp As Object

Dim Part                                As Object

Dim boolstatus                          As Boolean

Dim longstatus                          As Long, longwarnings As Long

Dim swSelMgr                            As SldWorks.SelectionMgr

Const SelectedObjectCount = 1

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

       

'Select the face to verify macro is workiing

        boolstatus = Part.Extension.SelectByID2("", "FACE", 0.304800000000057, 0.151591133586805, -3.67656190501293E-02, False, 0, Nothing, 0)

'Selection Setup

        Set swSelMgr = Part.SelectionManager

        Part.ClearSelection2 True

        MsgBox "Select a Face to to start a sketch on, then click OK."

        'frmSelectFace.Show

    'Wait while user selects a face and clicks OK

        While swSelMgr.GetSelectedObjectCount < SelectedObjectCount

        Wend

End Sub

thank you in advance for the help

Chris

SolidworksApi macros
Answers 8
JB
JB 2013-09-10 Best Answer

You at least need a DoEvents statement inside the loop.

What version of SolidWorks are you running?  If 2012 or earlier plus 64 bit, there may be an additional problem due to VBA being 32 bit and running in a separate process.  That caused all sorts of problems.

SF
SF 2013-09-10

Hi Josh,

SW 2012 X64 SP5.0

the DoEvents didnt fix it.  is there a fix for the additional problem you mentioned?

thank you

Chris

ST
ST 2013-09-10

I think you will either need to implement a PropertyManagerPage (fiddly), or ask the user to pre-select a face before running the macro (easier).

SF
SF 2013-09-10

Thanks Simon,

I guess I dont understand why it works when I run the macro from the editor but it doesnt work inside of SW.

for lack of a better solution I guess I could have all the end users run the macro from the editor but that just seems silly....

Surely I am missing something?

Thanks

Chris

JB
JB 2013-09-10

The simple "while loop" wait is pretty much out of the question with 64 bit SW and 32 bit VBA.  You will need to make a more complicated macro that registers to receive the SelectionChangeNotify (or somethign like that) event.  You may be able to rewrite in a VSTA .dll macro.... I'm not sure.  Simplest fix is to upgrade to 2013.  Simplest, that is, from a purely macro point of view.  I know there are lots of other factors that go into upgrading to the next release.  :-)

SF
SF 2013-09-10

Thanks Josh,

Wow, I had no idea this limitation existed I just spent a month learning vba and writing 800+ lines of code only to find out it wont work in 64 bit SW.

I suppose I can try to learn VSTA real quick and push everything over to that...

shouldn't be too hard to figure out if it will work.

thank you very much  for your help. even though it was horrible news I really appreciate you taking the time to reply

Thanks

Chris

JB
JB 2013-09-10

The good news is that it's a temporary limitation (assuming that you do get to eventually upgrade to 2013...)

SF
SF 2013-09-10

Agreed,

the only reason we aren't using 2013 now is because we still have people using windows XP in some parts of the world..

Apparently it takes a while to convert 35,000+ people to windows 7

at least that's what the slackers in IT are saying

Thanks