Hide Table of Contents

Get Sketch Regions Example (C#)

This example shows how to get the sketch regions in a sketch.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Open a model document that contains a Sketch1 feature with
//   one or more sketch regions.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Gets each sketch region.
// 2. Press F5 at each
System.Diagnostics.Debugger.Break statement.
// 3. Examine the Immediate window.
//----------------------------------------------------------------------------

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

namespace SelectSketchRegion_CSharp.csproj
{
    
partial class SolidWorksMacro
    {


        
public void Main()
        {
            
ModelDoc2 myModel = default(ModelDoc2);
            
PartDoc myPart = default(PartDoc);
            
SelectionMgr SelMgr = default(SelectionMgr);
            
SelectData mySelectData = default(SelectData);
            
Feature myFeature = default(Feature);
            
Sketch mySketch = default(Sketch);
            
int regionCount = 0;
            
object[] vSkRegions = null;
            
SketchRegion skRegion = default(SketchRegion);
            
Loop2 myLoop = default(Loop2);
            
int edgeCount = 0;
            
int vertexCount = 0;
            
object[] vEdges = null;
            
Edge myEdge = default(Edge);
            
object[] vVertices = null;
            
Vertex myVertex = default(Vertex);
            
double[] vPoint = null;
            
double X = 0;
            
double Y = 0;
            
double Z = 0;
            
bool outer = false;
            
string strOuter = null;
            
int i = 0;
            
int j = 0;
            
int k = 0;
            
bool boolstatus = false;

            myModel = (
ModelDoc2)swApp.ActiveDoc;
            SelMgr = (
SelectionMgr)myModel.SelectionManager;
            mySelectData = (
SelectData)SelMgr.CreateSelectData();
            myPart = (
PartDoc)myModel;
            myFeature = (
Feature)myPart.FeatureByName("Sketch1");
            mySketch = (
Sketch)myFeature.GetSpecificFeature2();
            
//             or
            //    Set mySketch = myModel.GetActiveSketch2()
            //    Set myFeature = mySketch

            if ((mySketch != null))
            {
                regionCount = mySketch.GetSketchRegionCount();
                
Debug.Print("");
                
Debug.Print(regionCount + " regions in sketch " + myFeature.Name);

                vSkRegions = (
object[])mySketch.GetSketchRegions();

                
for (i = vSkRegions.GetLowerBound(0); i <= vSkRegions.GetUpperBound(0); i++)
                {
                    skRegion = (
SketchRegion)vSkRegions[i];

                    
if ((skRegion != null))
                    {
                        
Debug.Print("  Region " + i + ":");
                        j = 0;
                        myLoop = skRegion.GetFirstLoop();
                        
while ((myLoop != null))
                        {
                            edgeCount = myLoop.GetEdgeCount();
                            vertexCount = myLoop.GetVertexCount();
                            outer = myLoop.IsOuter();

                            
if (outer == false)
                            {
                                strOuter =
"Inner loop";
                            }
                            
else
                            {
                                strOuter =
"Outer loop";
                            }

                            
Debug.Print("    Loop " + j + ": " + edgeCount + " edges, " + vertexCount + " vertices, " + strOuter);

                            vEdges = (
object[])myLoop.GetEdges();

                            
for (k = vEdges.GetLowerBound(0); k <= vEdges.GetUpperBound(0); k++)
                            {
                                myEdge = (
Edge)vEdges[k];
                                
if ((myEdge != null))
                                {
                                    
Debug.Print("      Edge " + k + ": ");
                                }

                            }

                            vVertices = (
object[])myLoop.GetVertices();

                            
for (k = vVertices.GetLowerBound(0); k <= vVertices.GetUpperBound(0); k++)
                            {
                                myVertex = (
Vertex)vVertices[k];
                                
if ((myVertex != null))
                                {
                                    vPoint = (
double[])myVertex.GetPoint();
                                    X = vPoint[0];
                                    Y = vPoint[1];
                                    Z = vPoint[2];

                                    
Debug.Print("      Vertex " + k + ": " + "(" + X + ", " + Y + ", " + Z + ")");
                                }
                            }

                            myLoop = (
Loop2)myLoop.GetNext();
                            j = j + 1;
                        }

                        boolstatus = skRegion.Select2(
false, mySelectData);
                        
if (boolstatus == false)
                        {
                            
Debug.Print("    Selection of region failed.");
                        }

                        System.Diagnostics.
Debugger.Break();

                    }

                }

            }

        }


        
public SldWorks swApp;

    }
}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Sketch Regions Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2017 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.