API Selections

Discussion in 'SolidWorks' started by cdubea, Jul 26, 2007.

  1. cdubea

    cdubea Guest

    Hi all,

    This is a message to the API guru's out there. I'm working on the
    next version of XMLPropWorks as discussed at http://www.dubea.org/xmlpropworks

    Anyway, the feature I'm currently working on is the ability to assign
    custom properties to a part within an assembly. What I'm looking for
    help with is identifying the part that the user has selected. I've
    gone through the documentation and there are plenty of functions to
    select a part by knowing it's name.

    What I want to know is how do I determine the name of the component
    that has had a feature selected by the user?

    Another option would be to force the user to edit the part within the
    confines of the assembly to be able to set the custom properties. In
    this case, how do I identify the edited part?

    Thanks

    chris

    NOTE: Please accept my apologies if you see multiple copies of this.
    I'm behind a firewall at work and am not certain that Agent is posting
    messages so I've posted this one from Google Groups.
     
    cdubea, Jul 26, 2007
    #1
  2. cdubea

    cdubea Guest

    Never mind.

    This is what I'm looking for (from the API help)


    'Get Selected Objects and Types Example (VB)
    'This example shows how to determine what is currently selected.

    '-----------------------------------------------
    ' Problem:
    ' There are many features and selectable
    ' entities within the SolidWorks software. In most cases,
    ' it is obvious as to what is selected but
    ' sometimes it is not clear or it is ambiguous.
    '
    ' This code sample is a diagnostic tool
    ' to determine what is currently selected. It shows
    ' several techniques and methods to get a reference
    ' to what is selected.
    '
    ' Preconditions:
    ' (1) Document is open.
    ' (2) Something is selected.
    '
    ' Postconditions:
    ' None
    '
    ' NOTE: Not all features have an associated *FeatureData object.
    '
    '-----------------------------------------------

    Option Explicit

    Public Enum swSelectType_e
    swSelNOTHING = 0
    swSelEDGES = 1 ' "EDGE"
    swSelFACES = 2 ' "FACE"
    swSelVERTICES = 3 ' "VERTEX"
    swSelDATUMPLANES = 4 ' "PLANE"
    swSelDATUMAXES = 5 ' "AXIS"
    swSelDATUMPOINTS = 6 ' "DATUMPOINT"
    swSelOLEITEMS = 7 ' "OLEITEM"
    swSelATTRIBUTES = 8 ' "ATTRIBUTE"
    swSelSKETCHES = 9 ' "SKETCH"
    swSelSKETCHSEGS = 10 ' "SKETCHSEGMENT"
    swSelSKETCHPOINTS = 11 ' "SKETCHPOINT"
    swSelDRAWINGVIEWS = 12 ' "DRAWINGVIEW"
    swSelGTOLS = 13 ' "GTOL"
    swSelDIMENSIONS = 14 ' "DIMENSION"
    swSelNOTES = 15 ' "NOTE"
    swSelSECTIONLINES = 16 ' "SECTIONLINE"
    swSelDETAILCIRCLES = 17 ' "DETAILCIRCLE"
    swSelSECTIONTEXT = 18 ' "SECTIONTEXT"
    swSelSHEETS = 19 ' "SHEET"
    swSelCOMPONENTS = 20 ' "COMPONENT"
    swSelMATES = 21 ' "MATE"
    swSelBODYFEATURES = 22 ' "BODYFEATURE"
    swSelREFCURVES = 23 ' "REFCURVE"
    swSelEXTSKETCHSEGS = 24 ' "EXTSKETCHSEGMENT"
    swSelEXTSKETCHPOINTS = 25 ' "EXTSKETCHPOINT"
    swSelHELIX = 26 ' "HELIX" (is this wrong?)
    swSelREFERENCECURVES = 26 ' "REFERENCECURVES"
    swSelREFSURFACES = 27 ' "REFSURFACE"
    swSelCENTERMARKS = 28 ' "CENTERMARKS"
    swSelINCONTEXTFEAT = 29 ' "INCONTEXTFEAT"
    swSelMATEGROUP = 30 ' "MATEGROUP"
    swSelBREAKLINES = 31 ' "BREAKLINE"
    swSelINCONTEXTFEATS = 32 ' "INCONTEXTFEATS"
    swSelMATEGROUPS = 33 ' "MATEGROUPS"
    swSelSKETCHTEXT = 34 ' "SKETCHTEXT"
    swSelSFSYMBOLS = 35 ' "SFSYMBOL"
    swSelDATUMTAGS = 36 ' "DATUMTAG"
    swSelCOMPPATTERN = 37 ' "COMPPATTERN"
    swSelWELDS = 38 ' "WELD"
    swSelCTHREADS = 39 ' "CTHREAD"
    swSelDTMTARGS = 40 ' "DTMTARG"
    swSelPOINTREFS = 41 ' "POINTREF"
    swSelDCABINETS = 42 ' "DCABINET"
    swSelEXPLVIEWS = 43 ' "EXPLODEDVIEWS"
    swSelEXPLSTEPS = 44 ' "EXPLODESTEPS"
    swSelEXPLLINES = 45 ' "EXPLODELINES"
    swSelSILHOUETTES = 46 ' "SILHOUETTE"
    swSelCONFIGURATIONS = 47 ' "CONFIGURATIONS"
    swSelOBJHANDLES = 48
    swSelARROWS = 49 ' "VIEWARROW"
    swSelZONES = 50 ' "ZONES"
    swSelREFEDGES = 51 ' "REFERENCE-EDGE"
    swSelREFFACES = 52
    swSelREFSILHOUETTE = 53
    swSelBOMS = 54 ' "BOM"
    swSelEQNFOLDER = 55 ' "EQNFOLDER"
    swSelSKETCHHATCH = 56 ' "SKETCHHATCH"
    swSelIMPORTFOLDER = 57 ' "IMPORTFOLDER"
    swSelVIEWERHYPERLINK = 58 ' "HYPERLINK"
    swSelMIDPOINTS = 59
    swSelCUSTOMSYMBOLS = 60 ' "CUSTOMSYMBOL"
    swSelCOORDSYS = 61 ' "COORDSYS"
    swSelDATUMLINES = 62 ' "REFLINE"
    swSelROUTECURVES = 63
    swSelBOMTEMPS = 64 ' "BOMTEMP"
    swSelROUTEPOINTS = 65 ' "ROUTEPOINT"
    swSelCONNECTIONPOINTS = 66 ' "CONNECTIONPOINT"
    swSelROUTESWEEPS = 67
    swSelPOSGROUP = 68 ' "POSGROUP"
    swSelBROWSERITEM = 69 ' "BROWSERITEM"
    swSelFABRICATEDROUTE = 70 ' "ROUTEFABRICATED"
    swSelSKETCHPOINTFEAT = 71 ' "SKETCHPOINTFEAT"
    swSelEMPTYSPACE = 72 ' (is this wrong?)
    swSelCOMPSDONTOVERRIDE = 72
    swSelLIGHTS = 73 ' "LIGHTS"
    swSelWIREBODIES = 74
    swSelSURFACEBODIES = 75 ' "SURFACEBODY"
    swSelSOLIDBODIES = 76 ' "SOLIDBODY"
    swSelFRAMEPOINT = 77 ' "FRAMEPOINT"
    swSelSURFBODIESFIRST = 78
    swSelMANIPULATORS = 79 ' "MANIPULATOR"
    swSelPICTUREBODIES = 80 ' "PICTURE BODY"
    swSelSOLIDBODIESFIRST = 81
    swSelDOWELSYMS = 86 ' "DOWELSYM"
    swSelEXTSKETCHTEXT = 88 ' "EXTSKETCHTEXT"
    swSelBLOCKINST = 93 ' "BLOCKINST"
    swSelCENTERMARKSYMS = 100 ' "CENTERMARKSYMS"
    swSelCENTERLINES = 103 ' "CENTERLINE"
    ' swSelEVERYTHING = 4294967293
    ' swSelLOCATIONS = 4294967294
    ' swSelUNSUPPORTED = 4294967295
    End Enum

    Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swSelObj As Object
    Dim swFeat As SldWorks.Feature
    Dim swEnt As SldWorks.Entity
    Dim swBody As SldWorks.Body2
    Dim swSelComp As SldWorks.Component2
    Dim swSelModel As SldWorks.ModelDoc2
    Dim nSelType As Long
    Dim sFeatName As String
    Dim bRet As Boolean

    ' Disables VB's implicit error on QI
    On Error Resume Next

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager

    ' Could either have a feature or entity selected
    '
    ' Do not try to get entity directly
    ' from feature because feature may be NULL. Instead,
    ' use SelectionMgr.
    Set swFeat = swSelMgr.GetSelectedObject5(1)
    Set swEnt = swSelMgr.GetSelectedObject5(1)
    Set swBody = swSelMgr.GetSelectedObject5(1)
    Set swSelObj = swSelMgr.GetSelectedObject5(1)

    Set swSelComp = swSelMgr.GetSelectedObjectsComponent2(1)

    Debug.Print "SelType = " & swSelMgr.GetSelectedObjectType2(1)

    If Not swFeat Is Nothing Then
    Debug.Print "Feature = " + swFeat.Name + " [" +
    swFeat.GetTypeName + "]"
    End If

    If Not swBody Is Nothing Then
    Debug.Print " Body selected"
    End If

    If swFeat Is Nothing And _
    swEnt Is Nothing And _
    swBody Is Nothing And _
    Not swSelObj Is Nothing Then
    Debug.Print " Unknown object"
    End If

    ' Could not get component from SelectionMgr
    ' so try and get component through Entity
    If swSelComp Is Nothing Then
    Set swSelComp = swEnt.GetComponent
    End If

    If Not swSelComp Is Nothing Then
    Set swSelModel = swSelComp.GetModelDoc

    Debug.Print "CompName = " + swSelComp.Name2
    Debug.Print "ModelPath = " + swSelModel.GetPathName
    End If
    End Sub
    '------------------------------------------

    Thanks

    Chris
     
    cdubea, Jul 26, 2007
    #2
  3. cdubea

    That70sTick Guest

    Selection sorting can be quite tedious, especially in assemblies.
    Often, a programmer wants to allow multiple ways of selection (by
    tree, entity, component). One ends up using a lot of calls to "get
    body from face", or "get sketch from selected line" or "get part from
    component ...from feature or face!".

    <http://www.esoxrepublic.com/freeware/index.html>
    Look at the "Density Manager" macro. It's getting a bit long in
    tooth, but I recall doing a lot of selection sorting in the program.
     
    That70sTick, Jul 26, 2007
    #3
  4. cdubea

    TOP Guest

    Why is the
    ON ERROR RESUME NEXT in there?

    TOP
     
    TOP, Jul 27, 2007
    #4
  5. cdubea

    That70sTick Guest

    So that errors don't stop the program.

    Look up error handling on the web somewhere (or maybe even in VB
    help). You can write code that tells VB how to handle errors (i.e.
    "On Error Goto MyErrorHandler")
     
    That70sTick, Jul 27, 2007
    #5
  6. cdubea

    TOP Guest

    I know what it does and have used it in various forms. I was wondering
    why it was in this code? What errors are expected to crop up.

    TOP
     
    TOP, Jul 28, 2007
    #6
  7. cdubea

    CS Guest

    this line is likely to throw an error if a document isn't opened

    Set swSelMgr = swModel.SelectionManager

    and if that fails you will have many following errors.
     
    CS, Aug 1, 2007
    #7
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.