Hide Table of Contents

Constrain Sketch Example (C#)

This example shows how to fully constrain a sketch.

 

Before constraining the sketch

After constraining the sketch

 

 

 

//----------------------------------------------------------------------------
// Preconditions: Before constraining the sketch sketch exists.
//
// Postconditions: Fully constrains the sketch, which looks like
// After constraining the sketch.
//----------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ConstrainSketch_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
ModelDoc2 swModel;
        
SketchManager swSketchMgr;
        
Sketch swSketch;
        
SelectionMgr swSelMgr;
        
Feature swFeat;
        
long nSketchStatus;
        
bool boolstatus;

        
public void Main()
        {
            swModel = (
ModelDoc2)swApp.ActiveDoc;

            
// Is a model document active?

            if (swModel == null)
            {
                swApp.SendMsgToUser2(
"A part document must be open and the active document.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
                
return;

            }

            
// Is it a part document?
            long modelType = 0;
            modelType = swModel.GetType();

            
if (modelType != (int)swDocumentTypes_e.swDocPART)
            {
                swApp.SendMsgToUser2(
"A part document must be open and the active document.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
                
return;

            }

            swSketchMgr = swModel.SketchManager;
            swSketch = swSketchMgr.ActiveSketch;

            
if (swSketch == null)
            {
                swApp.SendMsgToUser2(
"No active sketch; thus, a sketch could not be selected.", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk);
                
return;

            }

            
// Select the lines and make them colinear and vertical
            boolstatus = swModel.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0.02116924482339, 0.04904427527406, 0, false, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Line3", "SKETCHSEGMENT", 0.06508556638246, 0.02563976857491, 0, true, 0, null, 0);

            swModel.SketchAddConstraints(
"sgCOLINEAR");
            swModel.SketchAddConstraints(
"sgVERTICAL2D");

            
MessageBox.Show("The lines have been selected, made colinear, and vertically constrained.");
            swModel.ClearSelection2(
true);

            
//Select the center of the circles and constrain them to the origin
            boolstatus = swModel.Extension.SelectByID2("Point7", "SKETCHPOINT", 0.1074240560292, 0.006179841656516, 0, false, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, true, 0, null, 0);

            swModel.SketchAddConstraints(
"sgCOINCIDENT");
            
MessageBox.Show("The center of the circles and the origin were selected and made coincident");
            swModel.ClearSelection2(
true);

            
// Select a line and the circle and make them tangent
            boolstatus = swModel.Extension.SelectByID2("Line2", "SKETCHSEGMENT", 0.005390925700365, 0.009861449451888, 0, false, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Arc1", "SKETCHSEGMENT", -0.01222819732034, 0.04720347137637, 0, true, 0, null, 0);

            swModel.SketchAddConstraints(
"sgTANGENT");
            
MessageBox.Show("One line and a cirle were selected; both lines are now tangent with the circle.");
            swModel.ClearSelection2(
true);

            
//Select the circles and make them concentric
            boolstatus = swModel.Extension.SelectByID2("Arc2", "SKETCHSEGMENT", -0.0290584043849, 0.03116218026797, 0, false, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Arc1", "SKETCHSEGMENT", -0.01222819732034, 0.04720347137637, 0, true, 0, null, 0);

            swModel.SketchAddConstraints(
"sgCONCENTRIC");
            
MessageBox.Show("The circles have been selected and made concentric.");
            swModel.ClearSelection2(
true);

            
//Select all the sketch entities and fix their positions
            MessageBox.Show("All  sketch entities will be selected and made fixed to fully constrain the sketch.");
            boolstatus = swModel.Extension.SelectByID2(
"Line2", "SKETCHSEGMENT", 0.02116924482339, 0.04904427527406, 0, false, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Line3", "SKETCHSEGMENT", 0.06508556638246, 0.02563976857491, 0, true, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Arc2", "SKETCHSEGMENT", -0.0290584043849, 0.03116218026797, 0, false, 0, null, 0);
            boolstatus = swModel.Extension.SelectByID2(
"Arc1", "SKETCHSEGMENT", -0.01222819732034, 0.04720347137637, 0, true, 0, null, 0);

            swModel.SketchAddConstraints(
"sgFIXED");
            swModel.ClearSelection2(
true);

        }

        
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:   Constrain Sketch 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.