is it possible to cycle through user selected edges (like they were an array)?

I'm trying to write a macro to add reference points to multiple pre-selected edges.  I'd like to be able to cycle through so that i can add the points both evenly distributed along the edges and at the intersections. 

Basically, i'm pulling point clouds off of splines and exporting them to excel for manipulation.  I have the excel part down, but i'm currently manually adding the reference points.  Would like to be able to just select the multiple edges and have a steady distribution across all selected edges.

bonus points:  not really a math guy, but is there a way to determine if an edge is a more or less extreme curve?  I'd rather have more points on the "bendier" splines and fewer on the straight ones.  If not, I can live with that.

SolidworksApi macros
Answers 9
AJ
AJ 2016-07-23 Best Answer

I don't really understand your question, perhaps if you can clear it up with an example. That'll be delicious
You can certainly cycle through selected edges. Simply, each time, you select an edge, add to a collection object and iterate through it later. Face2::GetEdges will get you a list of edges.

Use the Edge::GetCurve method to the get the underlying curve for the edge!

PB
PB 2016-07-23

Are you working with splines only? I think circles are the easiest and writing separate functions for different curves would make this easier. Catching all options with one procedure can be tricky.

I suggest you do the following:

  • Select a curve
  • Start the macro
  • Make sure only one curve is selected, or loop through all curves
  • Get the selected curve into a variable
  • Get the type of curve so you can handle it accordingly
  • Get the length (GetLength) and shape
  • Assess the complexity of the curve, for example by calculating an average difference in direction of the spline tangents. If every tangent has a completely different direction, your curve might be complex.
  • Add points to the curve (in a 3D sketch probably) with a minimum and maximum amount of points per curve

You can use GetCurveParams3 to get the parameters of a curve (ID, type, start point, end point and U parameters). I can't find proper documentation on the U parameters, but it seems to be a parameter that describes how far along on the curve you where.

There are also tools on working with P-curves and B-splines, I'm sure you can figure out how to work with them yourself.

SF
SF 2016-07-25

I guess i was trying to approach it from this angle:

I select the curves that i want to create a point cloud on.

Run the macro.

     Macro cycles through each curve in turn and adds a number of points based on the length and "curviness" of the curve.

     Then macro spits out coordinates to excel. (this part i have working, i was just looking to automate the reference point generation.)

I guess the thing i'm having trouble seeing is how to cycle through a group of pre-selected curves (edges).  If I have 3 edges selected, how does solidworks handle this?   Is there an array that solidworks creates that i can cycle through (if so, how do i reference it?).

Sorry, i'm really new in this API and am still trying to figure out the SW object structure (methods/functions).

Unfortunately, these are client models that i'm picking up from .STP (through Catia).  A lot of these are older parts and are generally organically shaped (interior trim pieces for airplanes) so i pretty much have to treat everything as splines.

AJ
AJ 2016-07-25

Let me give you a hand. SolidWorks provides you with a selection manager interface.

If your edges are preselected then you would need to use GetSelectedObjectCount2 Method (ISelectionMgr) to the count of the selected objects. You use a for loop to cycle through them and to sort out those that are edges use the method GetSelectedObjectType3 Method (ISelectionMgr).

SF
SF 2016-07-25

This looks like it's the right track.  Thanks for the help!

SF
SF 2016-07-25

OK, i've tried this and it looks like it really wants to work (am getting the right selected count and selected type) but i'm now getting the "invoked object disconnected" error on the .insertreferencepoint line.    Here's what I've done so far:

Dim swApp As Object

Dim swModel As Object

Dim swSelMgr As SldWorks.SelectionMgr

Dim numSel As Integer

Dim selType As Integer

Dim vRefPoint As Variant

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swSelMgr = swModel.SelectionManager

numSel = swSelMgr.GetSelectedObjectCount2(-1)

For i = 1 To numSel

    selType = swSelMgr.GetSelectedObjectType3(i, -1)

    If selType = 1 Then

        vRefPoint = swModel.FeatureManager.InsertReferencePoint(2, 2, 0, 8)

    End If

Next i

End Sub

AJ
AJ 2016-07-25

Change 0 to 0#

and shouldn't it be one edge only at a time? You have more than one edge selected.

VB
VB 2016-07-25

Hi Sean. is right and only one edge need to be selected at a time for this operation. Try to use this code

Dim swApp As Object

Dim swModel As Object

Dim swSelMgr As SldWorks.SelectionMgr

Dim numSel As Integer

Dim selType As Integer

Dim vRefPoint As Variant

Sub main()

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Dim swSelData As SldWorks.SelectData

    Set swSelData = swSelMgr.CreateSelectData

   

    Dim egdeColl As New Collection

    numSel = swSelMgr.GetSelectedObjectCount2(-1)

    For i = 1 To numSel

        selType = swSelMgr.GetSelectedObjectType3(i, -1)

        If selType = 1 Then

            Dim edgeEntity As Entity

            Set edgeEntity = swSelMgr.GetSelectedObject6(i, 0)

            egdeColl.Add edgeEntity

        End If

    Next i

    For i = 1 To egdeColl.count

        egdeColl(i).Select4 False, swSelData

        vRefPoint = swModel.FeatureManager.InsertReferencePoint(2, 2, 0, 8)

    Next i

End Sub

Regards

Viktor

SF
SF 2016-07-26

This was the final piece of the puzzle!

Thank you for the help, gentlemen. This forum rocks.