SW API

Discussion in 'SolidWorks' started by P., Feb 12, 2005.

  1. P.

    P. Guest

    I have run into this situation before and haven't figured out the
    answer.

    I start with a simple recorded macro that picks an edge of a cylidrical
    surface and extends it. The methods that do the work are SelectByID and
    InsertExtendSurface.

    '
    ******************************************************************************
    ' C:\DOCUME~1\kellnerp\LOCALS~1\Temp\swx1568\Macro1.swb - macro
    recorded on 02/12/05 by kellnerp
    '
    ******************************************************************************
    Dim swApp As Object
    Dim Part As Object
    Dim boolstatus As Boolean
    Dim longstatus As Long, longwarnings As Long
    Dim FeatureData As Object
    Dim Feature As Object
    Dim Component As Object
    Sub main()

    Set swApp = Application.SldWorks

    Set Part = swApp.ActiveDoc
    boolstatus = Part.Extension.SelectByID("", "EDGE", -0.0104893598479,
    0.03815622208009, 0.1179905256888, False, 0, Nothing)
    Part.InsertExtendSurface 0, 0, 0.0811347

    End Sub

    So now I want to pick edges from separate surfaces and have
    InsertExtendSurface operate on them one by one. The problem I have is
    how to get InsertExtendSurface to see each selected edge apart from the
    others because it doesn't operate on more than one surface body at a
    time.
     
    P., Feb 12, 2005
    #1
  2. P.

    That70sTick Guest

    Macros aren't really a good way to record selections. You need to
    learn how to handle the Selection Manager object (see SW API help).
    The selection manager is basically a collection (list) of what is
    selected.

    For macro, there are basically two ways to handle selections:
    1.) pre-select objects prior to running the macro and have the
    selection manager sort through existing selections
    2.) show a form to prompt for selections. Make sure the form is shown
    "vbModeless"
    i.e.
    Form1.show vbModeless

    Showing the form modeless will allow the user to access the SW document
    window for selecting objects.
     
    That70sTick, Feb 12, 2005
    #2
  3. P.

    P. Guest

    I didn't post my attempt, but what I did was get the count of selected
    edges from the selection manager. The problem came when I tried to get
    the extend edge command to take just one of the selected edges using
    the GetSelecteObject5(i)method. I didn't want all of them because the
    extend edge command cannot operate on more than one body at a time.

    My preference would be to preselect the edges I want to extend before
    running the macro and have the macro just go through the edges one by
    one and extend them a set amount.
     
    P., Feb 13, 2005
    #3
  4. P.

    Tin Man Guest

    I have tried to use the "vbModeless" before, but all that happens is
    the Userform flashes on the screen and then it's gone. How do I get the
    Userfrom to stay on the screen *and* allow selection from the SW
    window?
    Thanks,
    Ken
     
    Tin Man, Feb 14, 2005
    #4
  5. P.

    CS Guest

    After the user pre-selects the edges you will want to use the selection
    manager to add them to a collection, since your selections will be lost with
    the creation of a new feature.

    Dim SelectedEdges as New Collection
    Dim i as variant

    For each i in SelMan.GetselectedObjects
    SelectedEdges.Add (i)
    next

    'now you can access each edge seperately in a similar loop

    For each i in SelectedEdges
    'code to create the extended surface features
    next


    Regards,

    Corey
     
    CS, Feb 14, 2005
    #5
  6. P.

    P. Guest

    OK, well a collection data type is new to me and of course that is not
    covered in the help. Now that I know what to look for I can peruse
    MSofts online VB docs.

    When I do
    For each i in SelectedEdges
    will that behave the same as selectbyID would in making the edge
    available to the feature definition method?

    I guess the point I am missing is how do various methods know what is
    selected? This is probably something I don't know that much about in
    programming with objects.
     
    P., Feb 14, 2005
    #6
  7. P.

    CS Guest

    The collection is going to store each object that is selected in the
    collection.

    A collection is similar to an array except it handles objects alot better
    and it is only 1 dimensional. And you are right in that you will not find
    anything in SWAPI help on collections. It is going to be found in VB docs
    as you thought

    In essence For each i in SelectedEdges will assign the objects stored in
    SelectedEdges one at a time to the variable i. You can then use them as you
    were in your former code.

    I was being lazy
    I don't know off hand how the methods you are using need to be supplied the
    objects. Most of the time SW relies on the selection order to determine
    what to do. So you will have to reselect the edges one at time using your
    collection to know what objects were origionally selected.

    In the first For each loop you may have to assign the edges to an edge
    object so that you can use them properly later. Unfortunately right know
    this is all the explaination I can give you. You may have to do some trial
    and error to get it working.
     
    CS, Feb 14, 2005
    #7
  8. Set the ShowModal property of your form to False

    Evan
     
    Evan T. Basalik, Feb 15, 2005
    #8
  9. P.

    P. Guest

    Tom Matheson on the Forum posted this. The key point I was missing was
    Select4 which apparently is what makes the entity selected as far as
    the ExtendEdge method is concerned.

    Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swPart As SldWorks.ModelDoc2
    Dim selmgr As SldWorks.SelectionMgr
    Dim extend_e() As Variant
    Dim id_vals() As Variant
    Set swApp = Application.SldWorks
    Set swPart = swApp.ActiveDoc
    Set selmgr = swPart.SelectionManager
    tot_ent = selmgr.GetSelectedObjectCount
    ReDim extend_e(tot_ent - 1)
    ReDim id_vals(tot_ent - 1)
    For a = 1 To tot_ent
    Set extend_e(a - 1) = selmgr.GetSelectedObject2(a)
    Set id_vals(a - 1) = selmgr.CreateSelectData
    Next
    For n = 0 To tot_ent - 1
    sel = extend_e(n).Select4(False, id_vals(n))
    swPart.InsertExtendSurface 0, 0, 0.0254
    Next
    End Sub
     
    P., Feb 15, 2005
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.