Offset Sketch Example (C#)
This example shows how to offset a sketch.
//----------------------------------------------------------------------------
// Preconditions: Ensure the specified template exists.
//
// Postconditions:
// 1. A part with a sketch of a line is created.
// 2. The sketch is offset 2.54 mm in both directions.
//
---------------------------------------------------------------------------
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;
namespace
OffsetSketch_CSharp.csproj
{
partial
class
SolidWorksMacro
{
ModelDoc2
Part;
SketchSegment
skSegment;
bool
boolstatus;
int
longstatus;
public
void Main()
{
Part = (ModelDoc2)swApp.NewDocument("C:\\ProgramData\\SolidWorks\\SolidWorks
2012\\templates\\Part.prtdot", 0, 0, 0);
swApp.ActivateDoc2("Part2",
false,
ref
longstatus);
Part = (ModelDoc2)swApp.ActiveDoc;
Part.SketchManager.InsertSketch(true);
boolstatus = Part.Extension.SelectByID2("Top
Plane",
"PLANE", -0.0770466366627886,
0.00233041566204965, 0.0390732100788036,
false, 0,
null, 0);
Part.ClearSelection2(true);
skSegment = Part.SketchManager.CreateLine(-0.081532,
0.028203, 0.0, -0.029228, -0.017264, 0.0);
skSegment = Part.SketchManager.CreateLine(-0.029228,
-0.017264, 0.0, 0.035382, -0.025468, 0.0);
skSegment = Part.SketchManager.CreateLine(0.035382,
-0.025468, 0.0, 0.087008, -0.070346, 0.0);
Part.ClearSelection2(true);
boolstatus = Part.Extension.SelectByID2("Line3",
"SKETCHSEGMENT",
0, 0, 0, false,
1, null,
0);
boolstatus = Part.SketchManager.SketchOffset(0.00254,
true,
true,
true,
false,
true);
// Alternative sketch offset
method
//
boolstatus = Part.SketchOffset2(0.00254, True, True)
Part.ClearSelection2(true);
Part.SketchManager.InsertSketch(true);
}
public
SldWorks
swApp;
}
}