I have a textbox which the user clicks on in a form. This triggers the MouseUp event which runs a Do Loop, waiting for a users selection. The selection is any object in SolidWorks. After the object is selected, I have a sub that determines if there is a feature and verifies that it is the correct type of feature we're needing. Next, I exit the Loop. I then trigger, by code, the MouseUp event for that textbox so focus remains on it. I want the user to be able to select a different feature without having to click on the textbox again. I've got this code to work.
What I want to do now is keep my selection shown in SolidWorks but still enter into my Do While Loop so another selection can be made (if the user so chooses to).
The problem I'm running into is this:
After making a selection for the first time, there will always be a selected object. If I'm doing GetSelectedObject(1,-1), then my code will never see a different selection made because it will always pick the already selected object; the object that was selected the first time. I want to keep my selection visible until I make a new selection.
How do I keep my selection in the graphics view while still being able to select a different feature?
Private Sub featTxtBox_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
'Selection needs to be cleared in order for the Do Loop to run,
'but I would still like to have the previously found valid object (if any) selected in 'the graphics view until a new selection is made
swModel.ClearSelection2 True
Do While swSelmgr.GetSelectedObjectCount < 1 'if there hasn't been a selection made yet, 'still loop
DoEvents
Set vSel = swSelmgr.GetSelectedObject6(1, -1) 'set the variable to the selection
If Not vSel Is Nothing Then 'if there has been a selection, then process it
'GetFeatSelection takes vSel and determines what type of object it is.
'The object needed displayed for this textbox is a feature, which could be selected 'by one of it's edges (object type 1), faces (object type 2), or by selecting the 'feature (object type 22) itself from the feature tree.
'If an edge or face is selected then GetFeatSelection determines who the owning 'parent is (which will be the feature that was intended to be selected).
'If the object type is not 1, 2, or 22....then the object is not a valid selection, 'set validSel = False and exit GetFeatSelection sub; Do Loop repeats again for a new 'selection.
GetSelections.GetFeatSelection vSel
If validSel = True Then 'if the selected object is a valid selection, display 'information
featTxtBox.Text = patternFeat.Name
termfeatTxtBox.BackColor = RGB(255, 255, 255)
termfeatTxtBox.Enabled = True
termfeatTxtBox.Text = txtboxStr
patternFeat.Select True 'selects the feature in the SolidWorks graphics window 'to visually show the selection
SelText featTxtBox, False, "select" 'highlight the name of the selection to 'visually show a change in the userform
Exit Do
End If
End If
Loop
'trigger a MouseUp event for featTxtBox to return focus to it so the user can make a 'different selection (if they want)
'without having to click in the textbox again.
featTxtBox_MouseUp 1, 0, x, y
End Sub
SolidworksApi/macros
The reason why I'm using a user form is because I want to mimic the side panel in SolidWorks when you create a feature such as a circular pattern. When you say pointer, are you referring to the mark or persistent ID?
When I first get into the MouseUp event, there is no last selected object. When I call the MouseUp event by code, the previous selection is still there. I guess I'm not fully understanding your suggestion.
Update:
If I change swSelmgr.GetSelectedObjectCount < 1 to swSelmgr.GetSelectedObjectCount < 2, my selected object does get selected and the first object unselected. But now the selection flashes. I think that this is because in my loop I'm selecting patternFeat and then somewhere in one of the sub routines I call, I clear the selections, then patternFeat gets selected in the loop again. I'll have to dig around in the sub routines and see if I can get rid of the swModel.ClearSelection2. No luck still.
Update2:
So after some serious debugging, I've added some checks and code and everything seems to be working. However, there's a bit of code that gets the selected object type which of course means that I have to have an object selected before hand. This little bit is causing a "flashing" since I'm cycling between this feature being selected and the mouse selected feature being selected by code.
The code below is part of a subroutine that is executed to get other information.
The feature that was mouse selected is an extrusion witch is extruded up to an endpoint of a line segment in a sketch. I've already determined which endpoint I should be looking at, which is AxisPt2. I get all the sketch relations of this point and find the entity that has a relationship with this point. I need to find what object type this entity is because there is a Select Case code block where I get information about this entity. To do this, the code selects the entity and then performs GetSelectedObjectType3(2,-1). Afterwards I deselect it. The flashing comes from this because I'm selecting the entity, then deselecting it, then re-selecting the extrusion feature...this loops. Is there a way to get an entities object type without selecting it? Maybe by using it's ID? I'm not familiar with a method.
vRelEntArr = AxisPt2.GetRelations
For Each vRelEnt In vRelEntArr
Set vEnt = vRelEnt
vDefEntArr = vEnt.GetDefinitionEntities2
vDefEntArr(0).Select True
swSelmgr.GetSelectedObjectType3(2, -1)
vDefEntArr(0).DeSelect
Select Case selVal
......
End Select
Next vRelEnt
Solved:
Before vDefEntArr(0).Select True, I set swModelView.EnableGraphicsUpdate = False. After, I set graphics updating to true. Easy solution.
Get the pointer to the last selected object, not the first. Call GetSelectedObjectCount and pass the result to your GetSelectedObject6. But in general it would be much better to do ehat you want with property manager page and selection box instead of the user form.