Select only Regular Sketch Elements

dave.laban
Posts: 321
Joined: Thu Mar 11, 2021 8:38 am
Answers: 5
x 48
x 390

Select only Regular Sketch Elements

Unread post by dave.laban »

I've got a large sketch that consists of regular and construction sketch elements. I need a way of selecting only the regular elements so that I can measure them all to get a total length figure. None of the selection filters seem to want to let me disregard construction geometry when selecting.

Anyone got any clever workarounds for just picking the regular sketch elements? I assume a macro would be able to do the job too?
image.png
by JSculley » Fri May 14, 2021 11:32 am
dave.laban wrote: Fri May 14, 2021 10:17 am I fear I may be misunderstanding something fundamental.
Indeed. My macro is a C# macro. You created a VBA macro and tried to paste in the C# code, which won't work. When you create a new macro, do you have the option of 'SW VSTA Macro' in the 'Save as type' drop down?

image.png
If not, you don't have the VSTA Tools installed and you'll need to use VBA. Here's the VBA macro:

Code: Select all

Dim swApp As Object


Sub main()
    Set swApp = Application.SldWorks
    Dim mdoc As ModelDoc2
    Dim mExt As ModelDocExtension
    Dim userUnit As userUnit
    Dim selMgr As SelectionMgr
    Dim skMgr As SketchManager
    Dim activeSketch As Sketch
    Dim sketchSegObjArray As Variant
    
    Set mdoc = swApp.ActiveDoc
    Set mExt = mdoc.Extension
    Set userUnits = mdoc.GetUserUnit(swUserUnitsType_e.swLengthUnit)
    Set selMgr = mdoc.SelectionManager
    Set skMgr = mdoc.SketchManager
    Set activeSketch = skMgr.activeSketch
    
    sketchSegObjArray = activeSketch.GetSketchSegments()
    Dim nextSeg As SketchSegment
    Dim sd As SelectData
    
    For i = 1 To UBound(sketchSegObjArray)
        Set nextSeg = sketchSegObjArray(i)
        If nextSeg.ConstructionGeometry = False Then
            Set sd = selMgr.CreateSelectData()
            sd.Mark = 0
            nextSeg.Select4 True, sd
        End If
    Next
            
    Dim meas As Measure
    Dim totalLengthMeters As Double
    Dim totalLengthUserUnitsString As String
    Set meas = mExt.CreateMeasure()
    meas.Calculate Nothing
    totalLengthMeters = meas.TotalLength
    totalLengthUserUnitsString = userUnits.ConvertToUserUnit(totalLengthMeters, True, True)
    swApp.SendMsgToUser2 "Total length: " & totalLengthUserUnitsString, swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk
    
End Sub
Go to full post
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 877

Re: Select only Regular Sketch Elements

Unread post by JSculley »

Here's a quick macro:


Code: Select all

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;

namespace SelSkeetchTest.csproj
{
    public partial class SolidWorksMacro
    {


        public void Main()
        {
            ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;
            ModelDocExtension mExt = mDoc.Extension;
            UserUnit userUnits = mDoc.GetUserUnit((int)swUserUnitsType_e.swLengthUnit) as UserUnit;
            SelectionMgr selMgr = mDoc.SelectionManager as SelectionMgr;
            SketchManager skMgr = mDoc.SketchManager;
            Sketch activeSketch = skMgr.ActiveSketch;
            object[] sketchSegObjArray = activeSketch.GetSketchSegments() as object[];
            foreach (object nextSegObj in sketchSegObjArray)
            {
                SketchSegment nextSeg = nextSegObj as SketchSegment;
                if (!nextSeg.ConstructionGeometry)
                {
                    SelectData sd = selMgr.CreateSelectData();
                    sd.Mark = 0;
                    nextSeg.Select4(true, sd);
                }
            }
            Measure measure = mExt.CreateMeasure();
            measure.Calculate(null);
            double totalLengthMeters = measure.TotalLength;
            string totalLengthUserUnitsString = userUnits.ConvertToUserUnit(totalLengthMeters, true, true);
            swApp.SendMsgToUser2("Total length: " + totalLengthUserUnitsString, (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbOk);
        }

        /// <summary>
        ///  The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
    }
}


dave.laban
Posts: 321
Joined: Thu Mar 11, 2021 8:38 am
Answers: 5
x 48
x 390

Re: Select only Regular Sketch Elements

Unread post by dave.laban »

Thanks!

I've just tried pasting that in and it doesn't seem to like something as it's coming up as a wall of red text;
image.png
And when I try to add a button to the UI the following error message is returned;
image.png
Any thoughts?
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 877

Re: Select only Regular Sketch Elements

Unread post by JSculley »

Your macro is named differently so the namespace is messing things up. Just create a new macro and paste the code from my Main() method into your Main() method instead of cut and pasting the entire thing.
dave.laban
Posts: 321
Joined: Thu Mar 11, 2021 8:38 am
Answers: 5
x 48
x 390

Re: Select only Regular Sketch Elements

Unread post by dave.laban »

I fear I may be misunderstanding something fundamental. Have pasted in from Main() only and it's still a flood of red;
image.png
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 877

Re: Select only Regular Sketch Elements

Unread post by JSculley »

dave.laban wrote: Fri May 14, 2021 10:17 am I fear I may be misunderstanding something fundamental.
Indeed. My macro is a C# macro. You created a VBA macro and tried to paste in the C# code, which won't work. When you create a new macro, do you have the option of 'SW VSTA Macro' in the 'Save as type' drop down?

image.png
If not, you don't have the VSTA Tools installed and you'll need to use VBA. Here's the VBA macro:

Code: Select all

Dim swApp As Object


Sub main()
    Set swApp = Application.SldWorks
    Dim mdoc As ModelDoc2
    Dim mExt As ModelDocExtension
    Dim userUnit As userUnit
    Dim selMgr As SelectionMgr
    Dim skMgr As SketchManager
    Dim activeSketch As Sketch
    Dim sketchSegObjArray As Variant
    
    Set mdoc = swApp.ActiveDoc
    Set mExt = mdoc.Extension
    Set userUnits = mdoc.GetUserUnit(swUserUnitsType_e.swLengthUnit)
    Set selMgr = mdoc.SelectionManager
    Set skMgr = mdoc.SketchManager
    Set activeSketch = skMgr.activeSketch
    
    sketchSegObjArray = activeSketch.GetSketchSegments()
    Dim nextSeg As SketchSegment
    Dim sd As SelectData
    
    For i = 1 To UBound(sketchSegObjArray)
        Set nextSeg = sketchSegObjArray(i)
        If nextSeg.ConstructionGeometry = False Then
            Set sd = selMgr.CreateSelectData()
            sd.Mark = 0
            nextSeg.Select4 True, sd
        End If
    Next
            
    Dim meas As Measure
    Dim totalLengthMeters As Double
    Dim totalLengthUserUnitsString As String
    Set meas = mExt.CreateMeasure()
    meas.Calculate Nothing
    totalLengthMeters = meas.TotalLength
    totalLengthUserUnitsString = userUnits.ConvertToUserUnit(totalLengthMeters, True, True)
    swApp.SendMsgToUser2 "Total length: " & totalLengthUserUnitsString, swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk
    
End Sub
dave.laban
Posts: 321
Joined: Thu Mar 11, 2021 8:38 am
Answers: 5
x 48
x 390

Re: Select only Regular Sketch Elements

Unread post by dave.laban »

Just checked and I don't have the VSTA Tools installed, so mystery solved! I have copied your VBA code and it has worked perfectly, many thanks!

As a side topic, what advantage does creating a C# macro have over a VBA one? Or is it just a programming language preference?
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 877

Re: Select only Regular Sketch Elements

Unread post by JSculley »

Under the covers, all SOLIDWORKS macros (regardless of language) are using Microsoft COM (Component Object Model). So there is no real performance advantage based on language chosen. However, VBA is essentially a dead technology. It will see no changes or improvements moving forward. The .NET languages such as VB.NET and C# continue to evolve and improve and have a huge number of built-in and 3rd party libraries you can use.

If you have ever used a more modern object oriented language like C#, VBA just seems clunky and inconsistent. If you take the time to learn the basics of C# (or VB.NET) you have a more solid foundation on which to build your programming skills that can be useful with other languages. Before using C#, my experience was mostly Java. The transition between the two was pretty easy. If you can read/write in one you can read/write in the other. If you know VBA, well, you know VBA.
Post Reply