Repository URL to install this package:
|
Version:
2016.3.0.4050 ▾
|
draftsight
/
opt
/
dassault-systemes
/
DraftSight
/
APISDK
/
Create_and_Apply_DimensionStyle_Example_CSharp.htm
|
|---|
<html>
<head>
<title>Create and Apply DimensionStyle Example (C#)</title>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="ApiHelp.css">
<meta name=MS-HKWD content="C# examples">
<meta name=MS-HKWD content="C# examples,DimensionStyles">
<meta name=MS-HKWD content="C# examples,Dimensions">
<meta name=MS-HKWD content="DimensionStyle, create">
<meta name=MS-HKWD content="DimensionStyle, activate">
<meta name=MS-HKWD content="DimensionStyle, set">
<meta name=MS-HKWD content="Dimension, jogged">
<meta name=MS-HKWD content="Dimension, arc length">
<meta name=MS-HKWD content="Dimension, rotated">
<meta name=MS-HKWD content="Dimension, radius">
<meta name=MS-HKWD content="Dimension, diameter">
<meta name=MS-HKWD content="Dimension, general">
<meta name=MS-HKWD content="Dimension">
<meta name=MS-HKWD content="Diameter Dimension">
<meta name=MS-HKWD content="Jogged Dimension">
<meta name=MS-HKWD content="Radius Dimension">
<meta name=MS-HKWD content="Rotated Dimension">
<meta name=MS-HKWD content="C# examples,arc length Dimensions">
<meta name=MS-HKWD content="C# examples,jogged Dimensions">
<meta name=MS-HKWD content="C# examples,radius Dimensions">
<meta name=MS-HKWD content="C# examples,diameter Dimensions">
<meta name=MS-HKWD content="C# examples,rotated Dimensions">
</head>
<body>
<h1>Create and Apply DimensionStyle Example (C#)</h1>
<font SIZE="2">
<p>This example shows how to create, activate, and apply a new DimensionStyle.
This example also shows how to create arc length, jogged, rotated, radius, and
diameter Dimensions for circles, arcs, and a line.</p>
</font>
<pre>//-------------------------------------------------------------
// Preconditions:
// 1. Create a C# Windows console project.
// 2. Copy and paste this example into the C# IDE.
// 3. Add a reference to:
// <i>install_dir</i>\<b>APISDK\tlb\DraftSight.Interop.dsAutomation.dll</b>
// 4. Add references to System and System.Windows.Forms.
// 5. Set a breakpoint at:
// dsApp.Zoom(dsZoomRange_e.dsZoomRange_Fit, null, null);
// 6. Start DraftSight and open a document.
// 7. Click Start Debugging.
//
// Postconditions:
// 1. A new DimensionStyle named SampleDimStyle is created and
// activated.
// 2. Arc length, jogged, rotated, radius, and diameter dimensions
// are created for circles, arcs, and a line, using the new
// DimensionStyle.
// 3. Examine the Output or Immediate window.
// 4. Click Continue.
// 5. Examine the drawing.
//------------------------------------------------------------</pre>
<pre>using System;
using DraftSight.Interop.dsAutomation;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;</pre>
<pre>namespace AddArcLengthAndJoggedDimension
{
class Program
{
private static DraftSight.Interop.dsAutomation.Application dsApp;</pre>
<pre> static void Main(string[] args)
{
//Connect to DraftSight application
dsApp = (DraftSight.Interop.dsAutomation.Application)Marshal.GetActiveObject("DraftSight.Application");
dsApp.AbortRunningCommand(); // abort any command currently running in DraftSight to avoid nested commands</pre>
<pre> //Get active document
Document dsDoc = dsApp.<b>GetActiveDocument</b>();
if (null == dsDoc)
{
MessageBox.Show("There are no open documents in DraftSight.");
return;
}</pre>
<pre> //Get DimensionStyle manager
DimensionStyleManager dsDimStyleManager = dsDoc.<b>GetDimensionStyleManager</b>();</pre>
<pre> //Create DimensionStyle named SampleDimStyle
dsCreateObjectResult_e createDimStyleResult;
string dimStyleName = "SampleDimStyle";
DimensionStyle dsDimStyle;
dsDimStyleManager.<b>CreateDimensionStyle</b>(dimStyleName, out dsDimStyle, out createDimStyleResult);
if(dsCreateObjectResult_e.dsCreateObjectResult_Error == createDimStyleResult ||
dsCreateObjectResult_e.dsCreateObjectResult_AlreadyExists == createDimStyleResult ||
null == dsDimStyle)
{
MessageBox.Show("Failed to create " + dimStyleName + " DimensionStyle, or DimensionStyle already exists.");
return;
}</pre>
<pre> SetDimensionStyleSettings(dsDimStyle);</pre>
<pre> //Activate DimensionStyle
dsDimStyle.<b>Activate</b>(); </pre>
<pre> //Get model space
Model dsModel = dsDoc.<b>GetModel</b>();</pre>
<pre> //Get sketch manager
SketchManager dsSketchMgr = dsModel.<b>GetSketchManager</b>();</pre>
<pre> //Draw arc length Dimension
DrawArcLengthDimension(dsSketchMgr); </pre>
<pre> //Draw jogged Dimension for circle and arc
DrawJoggedDimension(dsSketchMgr);</pre>
<pre> //Draw rotated Dimension
DrawRotatedDimension(dsSketchMgr);</pre>
<pre> //Draw radius Dimension for circle and arc
DrawRadialDimension(dsSketchMgr);</pre>
<pre> //Draw diameter Dimension for circle and arc
DrawDiameterDimension(dsSketchMgr);</pre>
<pre> //Zoom to fit
dsApp.<b>Zoom</b>(dsZoomRange_e.dsZoomRange_Fit, null, null);
} </pre>
<pre> private static void SetDimensionStyleSettings(DimensionStyle dsDimStyle)
{
//Get DimensionStyle arrows options
DimensionStyleArrowsOptions dsArrowsDimStyleOptions = dsDimStyle.<b>GetDimensionStyleArrowsOptions</b>();</pre>
<pre> //Set start and end arrow types
dsArrowsDimStyleOptions.<b>SetStartArrow</b>(dsDimensionArrowType_e.dsDimensionArrowType_ClosedBlank, string.Empty);
dsArrowsDimStyleOptions.<b>SetEndArrow</b>(dsDimensionArrowType_e.dsDimensionArrowType_ClosedBlank, string.Empty);</pre>
<pre> //Get DimensionStyle line options
DimensionStyleLineOptions dsLineDimStyleOptions = dsDimStyle.<b>GetDimensionStyleLineOptions</b>();</pre>
<pre> //Set Dimension line color
Color dsColor = dsLineDimStyleOptions.<b>DimensionLineColor</b>;
dsColor.<b>SetNamedColor</b>(dsNamedColor_e.dsNamedColor_Green);
dsLineDimStyleOptions.<b>DimensionLineColor</b> = dsColor;</pre>
<pre> //Set extension line color
dsColor.<b>SetNamedColor</b>(dsNamedColor_e.dsNamedColor_Yellow);
dsLineDimStyleOptions.<b>ExtensionLineColor</b> = dsColor;</pre>
<pre> //Get DimensionStyle radius and diameter Dimension options
DimensionStyleRadialDiameterDimensionOptions dsRadialAndDiameterDimStyleOptions = dsDimStyle.<b>GetDimensionStyleRadialDiameterDimensionOptions</b>();</pre>
<pre> //Set jog angle 45 degrees (in radians)
dsRadialAndDiameterDimStyleOptions.<b>RadiusDimensionJogAngle</b> = Math.PI / 4;
</pre>
<pre> //Set center mark
double markSize = 0.05;
dsRadialAndDiameterDimStyleOptions.<b>SetCenterMarkDisplay</b>(dsDimensionCenterMarkDisplay_e.dsDimensionCenterMarkDisplay_AsMark, markSize);</pre>
<pre> //Get Dimension style text options
DimensionStyleTextOptions dsTextOptions = dsDimStyle.<b>GetDimensionStyleTextOptions</b>();
</pre>
<pre> //Frame Dimension text
dsTextOptions.<b>FrameDimensionText</b> = true;</pre>
<pre> //Set text position
dsTextOptions.<b>HorizontalPosition</b> = dsDimensionTextHorizontalPosition_e.dsDimensionTextHorizontalPosition_Centered;
dsTextOptions.<b>VerticalPosition</b> = dsDimensionTextVerticalPosition_e.dsDimensionTextVerticalPosition_Centered;</pre>
<pre> //Set text alignment
dsTextOptions.<b>Alignment</b> = dsDimensionTextAlignment_e.dsDimensionTextAlignment_AlignWithDimensionLines;
}</pre>
<pre> private static void DrawArcLengthDimension(SketchManager dsSketchMgr)
{
//Add arc to drawing
double centerX = -8;
double centerY = 1;
double centerZ = 0;
double radius = 5;
double startAngle = Math.PI / 6;
double endAngle = Math.PI;
CircleArc dsArc = dsSketchMgr.<b>InsertArc</b>(centerX, centerY, centerZ, radius, startAngle, endAngle);</pre>
<pre> //Add arc length Dimension
double[] dimensionPosition = new double[] { -6, 6, 0 };
string dimensionTextOverride = string.Empty;
ArcLengthDimension dsArcLengthDim = dsSketchMgr.<b>InsertArcLengthDimension</b>(dsArc, dimensionPosition, dimensionTextOverride);</pre>
<pre> //Print information about arc length Dimension
PrintArcLengthDimProperties(dsArcLengthDim);</pre>
<pre> //Add a partial arc length Dimension
double[] firstPoint = new double[] { -4, 3, 0 };
double[] secondPoint = new double[] { -7, 6, 0 };
dimensionPosition[0] = -6;
dimensionPosition[1] = 7;
ArcLengthDimension dsArcLengthPartialDim = dsSketchMgr.<b>InsertArcLengthDimensionPartial</b>(dsArc, firstPoint, secondPoint, dimensionPosition, dimensionTextOverride);</pre>
<pre> //Print information about partial arc length Dimension
PrintArcLengthDimProperties(dsArcLengthDim);
}</pre>
<pre> private static void DrawJoggedDimension(SketchManager dsSketchMgr)
{
//Draw a circle
double centerX = -7, centerY = -5, centerZ = 0;
double radius = 3;
Circle dsCircle = dsSketchMgr.<b>InsertCircle</b>(centerX, centerY, centerZ, radius); </pre>
<pre> //Add jogged Dimension to circle
double[] centerPositionOverride = new double[] { -12, -8, 0 };
double[] jogLinePosition = new double[] { -11, -8, 0 };
double[] dimensionPosition = new double[] { -10, -7.5, 0 };
string dimensionTextOverride = string.Empty;
JoggedDimension dsJoggedDimForCircle = dsSketchMgr.<b>InsertJoggedDimensionCircle</b>(dsCircle, centerPositionOverride, jogLinePosition, dimensionPosition, dimensionTextOverride);</pre>
<pre> //Print information about jogged Dimension
PrintJoggedDimProperties(dsJoggedDimForCircle);</pre>
<pre> //Draw an arc
centerX = 2;
centerY = -6;
double arcRadius = 3;
double startAngle = 0.0;
double endAngle = Math.PI / 3;
CircleArc dsArc = dsSketchMgr.<b>InsertArc</b>(centerX, centerY, centerZ, arcRadius, startAngle, endAngle);</pre>
<pre> //Add jogged Dimension to arc
centerPositionOverride[0] = 7;
centerPositionOverride[1] = -3;
jogLinePosition[0] = 7;
jogLinePosition[1] = -4;
dimensionPosition[0] = 5.5;
dimensionPosition[1] = -4.5;
JoggedDimension dsJoggedDimForArc = dsSketchMgr.<b>InsertJoggedDimensionArc</b>(dsArc, centerPositionOverride, jogLinePosition, dimensionPosition, dimensionTextOverride);</pre>
<pre> //Print information about jogged Dimension
PrintJoggedDimProperties(dsJoggedDimForArc);
} </pre>
<pre> private static void DrawRadialDimension(SketchManager dsSketchMgr)
{
//Draw a circle
double centerX = 2, centerY = 2, centerZ = 0;
double radius = 3;
Circle dsCircle = dsSketchMgr.<b>InsertCircle</b>(centerX, centerY, centerZ, radius);</pre>
<pre> //Draw an arc
centerX = 10;
centerY = 2;
double arcRadius = 3;
double startAngle = 0.0;
double endAngle = Math.PI / 3;
CircleArc dsArc = dsSketchMgr.<b>InsertArc</b>(centerX, centerY, centerZ, arcRadius, startAngle, endAngle);</pre>
<pre> //Add radius Dimension to circle
double[] dimPosition = new double[] { 7, 6, 0 };
string dimTextOverride = string.Empty;
RadialDimension dsRadialCircleDim = dsSketchMgr.<b>InsertRadialDimensionCircle</b>(dsCircle, dimPosition, dimTextOverride);</pre>
<pre> //Print information about radius Dimension
PrintRadialDimProperties(dsRadialCircleDim);</pre>
<pre> //Add radius Dimension to arc
dimPosition[0] = 16;
dimPosition[1] = 3;
RadialDimension dsRadialArcDim = dsSketchMgr.<b>InsertRadialDimensionArc</b>(dsArc, dimPosition, dimTextOverride);</pre>
<pre> //Print information about radius Dimension
PrintRadialDimProperties(dsRadialArcDim);
}</pre>
<pre> private static void DrawDiameterDimension(SketchManager dsSketchMgr)
{
//Draw a circle
double centerX = 2, centerY = 2, centerZ = 0;
double radius = 3;
Circle dsCircle = dsSketchMgr.InsertCircle(centerX, centerY, centerZ, radius);</pre>
<pre> //Draw an arc
centerX = 10;
centerY = 2;
double arcRadius = 3;
double startAngle = 0.0;
double endAngle = Math.PI / 3;
CircleArc dsArc = dsSketchMgr.<b>InsertArc</b>(centerX, centerY, centerZ, arcRadius, startAngle, endAngle);</pre>
<pre> //Add diameter Dimension to circle
double[] dimPosition = new double[] { 3, 8, 0 };
//No text override - empty string
string dimTextOverride = string.Empty;
DiameterDimension dsDiameterCircleDim = dsSketchMgr.<b>InsertDiameterDimensionCircle</b>(dsCircle, dimPosition, dimTextOverride);</pre>
<pre> //Print information about diameter Dimension
PrintDiameterDimProperties(dsDiameterCircleDim);</pre>
<pre> //Add diameter Dimension to arc
dimPosition[0] = 14;
dimPosition[1] = 6;
DiameterDimension dsDiameterArcDim = dsSketchMgr.<b>InsertDiameterDimensionArc</b>(dsArc, dimPosition, dimTextOverride);</pre>
<pre> //Print information about diameter Dimension
PrintDiameterDimProperties(dsDiameterArcDim);
}</pre>
<pre> private static void DrawRotatedDimension(SketchManager dsSketchMgr)
{
//Draw line
double startX = 10;
double startY = -5;
double startZ = 0;
double endX = 14;
double endY = -5;
double endZ = 0;
Line dsLine = dsSketchMgr.<b>InsertLine</b>(startX, startY, startZ, endX, endY, endZ);</pre>
<pre> //Draw rotated Dimension
double[] extLine1Point = new double[] {startX, startY, startZ };
double[] extLine2Point = new double[] { endX, endY, endZ };
double[] dimensionLinePosition = new double[] { 16, -6, 0 };
string dimTextOverride = string.Empty;</pre>
<pre> //Angle 45 degrees (in radians)
double rotationAngle = Math.PI / 4;
RotatedDimension dsRotatedDim = dsSketchMgr.<b>InsertRotatedDimension</b>(extLine1Point, extLine2Point, dimensionLinePosition, dimTextOverride, rotationAngle);</pre>
<pre> //Print information about rotated Dimension
PrintRotatedDimProperties(dsRotatedDim);
}</pre>
<pre> private static void PrintArcLengthDimProperties(ArcLengthDimension dsArcLengthDim)
{
Debug.Print(Environment.NewLine + " Arc length Dimension parameters...");</pre>
<pre> //Get general Dimension object, which contains common Dimension properties,
//and print them
GeneralDimension dsGeneralDim = dsArcLengthDim.<b>GetGeneralDimension</b>();
PrintGeneralDimProperties(dsGeneralDim);</pre>
<pre> //Print specific parameters for arc length Dimension
Debug.Print(" ArcSymbolType = " + dsArcLengthDim.<b>ArcSymbolType</b>);
Debug.Print(" HasLeader = " + dsArcLengthDim.<b>HasLeader</b>);
Debug.Print(" IsPartial = " + dsArcLengthDim.<b>IsPartial</b>);
</pre>
<pre> double x, y, z;
//Get center point
dsArcLengthDim.<b>GetCenterPoint</b>(out x, out y, out z);
Debug.Print(" Center point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get arc point
dsArcLengthDim.<b>GetArcPoint</b>(out x, out y, out z);
Debug.Print(" Arc point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get extension line 1 point
dsArcLengthDim.<b>GetExtensionLine1Point</b>(out x, out y, out z);
Debug.Print(" Extension line 1 point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get extension line 2 point
dsArcLengthDim.<b>GetExtensionLine2Point</b>(out x, out y, out z);
Debug.Print(" Extension line 2 point (" + x + "," + y + "," + z + ")");
}</pre>
<pre> private static void PrintRadialDimProperties(RadialDimension dsRadialDim)
{
Debug.Print(Environment.NewLine + " Radius Dimension parameters...");</pre>
<pre> //Get general Dimension object, which contains common Dimension properties,
//and print them
GeneralDimension dsGeneralDim = dsRadialDim.<b>GetGeneralDimension</b>();
PrintGeneralDimProperties(dsGeneralDim);</pre>
<pre> //Print specific parameters for radius Dimension
double x, y, z;
//Get center point
dsRadialDim.<b>GetCenterPoint</b>(out x, out y, out z);
Debug.Print(" Center point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get defining point
dsRadialDim.<b>GetDefiningPoint</b>(out x, out y, out z);
Debug.Print(" Defining point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Print leader length value
Debug.Print(" Leader length = " + dsRadialDim.<b>LeaderLength</b>);
}</pre>
<pre> private static void PrintDiameterDimProperties(DiameterDimension dsDiameterDim)
{
Debug.Print(Environment.NewLine + " Diameter Dimension parameters...");</pre>
<pre> //Get general Dimension object, which contains common Dimension properties,
//and print them
GeneralDimension dsGeneralDim = dsDiameterDim.<b>GetGeneralDimension</b>();
PrintGeneralDimProperties(dsGeneralDim);</pre>
<pre> //Print specific parameters for diameter Dimension
double x, y, z;
//Get defining point
dsDiameterDim.GetDefiningPoint(out x, out y, out z);
Debug.Print(" Defining point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get far defining point
dsDiameterDim.GetFarDefiningPoint(out x, out y, out z);
Debug.Print(" Far defining point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Print leader length value
Debug.Print(" Leader length = " + dsDiameterDim.<b>LeaderLength</b>);
}</pre>
<pre> private static void PrintJoggedDimProperties(JoggedDimension dsJoggedDim)
{
Debug.Print(Environment.NewLine + " Jogged Dimension parameters...");</pre>
<pre> //Get general Dimension object, which contains common Dimension properties,
//and print them
GeneralDimension dsGeneralDim = dsJoggedDim.<b>GetGeneralDimension</b>();
PrintGeneralDimProperties(dsGeneralDim);</pre>
<pre> //Print specific parameters for jogged Dimension
Debug.Print(" Jog angle = " + dsJoggedDim.<b>JogAngle</b>);</pre>
<pre> double x, y, z;
//Get center point
dsJoggedDim.<b>GetCenterPoint</b>(out x, out y, out z);
Debug.Print(" Center point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get chord point
dsJoggedDim.<b>GetChordPoint</b>(out x, out y, out z);
Debug.Print(" Chord point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get jog point
dsJoggedDim.<b>GetJogPoint</b>(out x, out y, out z);
Debug.Print(" Jog point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get override center point
dsJoggedDim.<b>GetOverrideCenterPoint</b>(out x, out y, out z);
Debug.Print(" Override center point (" + x + "," + y + "," + z + ")");
}</pre>
<pre> private static void PrintRotatedDimProperties(RotatedDimension dsRotatedDim)
{
Debug.Print(Environment.NewLine + " Rotated Dimension parameters...");</pre>
<pre> //Get general Dimension object, which contains common Dimension properties,
//and print them
GeneralDimension dsGeneralDim = dsRotatedDim.<b>GetGeneralDimension</b>();
PrintGeneralDimProperties(dsGeneralDim);</pre>
<pre> //Print specific parameters for rotated Dimension
Debug.Print(" Rotation angle = " + dsRotatedDim.<b>Rotation</b>);</pre>
<pre> double x, y, z;
//Get Dimension line point
dsRotatedDim.<b>GetDimensionLinePoint</b>(out x, out y, out z);
Debug.Print(" Dimension line point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get extension line 1 point
dsRotatedDim.<b>GetExtensionLine1Point</b>(out x, out y, out z);
Debug.Print(" Extension line 1 point (" + x + "," + y + "," + z + ")");</pre>
<pre> //Get extension line 2 point
dsRotatedDim.<b>GetExtensionLine2Point</b>(out x, out y, out z);
Debug.Print(" Extension line 2 point (" + x + "," + y + "," + z + ")");
}</pre>
<pre> private static void PrintGeneralDimProperties(GeneralDimension dsGeneralDim)
{
//Get general Dimension object, which contains common Dimension properties,
//and print them
Debug.Print(" Dimension style = " + dsGeneralDim.<b>DimensionStyle</b>);
Debug.Print(" Handle = " + dsGeneralDim.<b>Handle</b>);
Debug.Print(" Measurement = " + dsGeneralDim.<b>Measurement</b>.ToString());
Debug.Print(" Related = " + dsGeneralDim.<b>Related</b>.ToString());
Debug.Print(" Text override = " + dsGeneralDim.<b>TextOverride</b>);
Debug.Print(" TextRotation = " + dsGeneralDim.<b>TextRotation</b>);</pre>
<pre> //Get text position
double x, y;
dsGeneralDim.<b>GetTextPosition</b>(out x, out y);
Debug.Print(" Text position (" + x + "," + y + ")");
}
}
}</pre>
<p> </p>
</body>
</html>