This example shows how to create graphic objects using the tool for capturing point coordinates on a graphic document.
In the example, a spiral is created by acquiring three points that identify the center, the spacing between loops, and the radius of the spiral.
The ToolPickPoint tool is activated a first time to capture the center of the spiral and the spacing between loops. Then the ToolPickPoint tool is activated again to acquire the radius of the spiral by setting the first point in the center of the spiral.
Example
Copy code | |
---|---|
// This script draws an Archimedean spiral.
function OnMenu()
{
// Adds the command in the menu for graphic documents.
DlxApp.GetMenu(DlxApp.MENU_DWG).AddCommand("Draw Spiral", 100, "Activate the tool to draw a spiral. ");
// Adds the command in the menu for schematics.
DlxApp.GetMenu(DlxApp.MENU_SCH).AddCommand("Draw Spiral", 100, "Activate the tool to draw a spiral. ");
}
function OnMenuUpdateCommand(cmd)
{
switch (cmd.GetCommandId())
{
case 100:
{
var layer = DlxApp.GetActiveLayer();
cmd.Enable(layer.IsValid() && (layer.GetType() == DlxApp.LAYERTYPE_DRAWING));
}; break;
}
}
function OnMenuCommand(cmdId)
{
switch (cmdId)
{
case 100:
DrawSpiral();
break;
}
}
function OnToolPickPoint(toolId, points)
{
if (toolId == 0)
{
// The tool to capture the center of the spiral and the distance between loops.
if (points.length == 1)
DlxVariables.SetVar("ToolPickPoint_Msg", DlxApp.Format("Center of the spiral: X=%.3f Y=%.3f", points[0].x, points[0].y));
else
DlxVariables.SetVar("ToolPickPoint_Msg", DlxApp.Format("Distance between loops: %.3f", points[0].Distance(points[1])));
}
if (toolId == 1)
{
// The tool to acquire the spiral radius.
var shape = DlxVariables.GetVar("shape");
var radius = points[0].Distance(points[1]);
var centerAndSpacing = DlxVariables.GetVar("centerAndSpacing");
DlxVariables.SetVar("ToolPickPoint_Msg", DlxApp.Format("Radius of the spiral: %.3f", radius));
BuildArchimedeanSpiral(shape, centerAndSpacing.p1, centerAndSpacing.Length(), points[0].ArcAngle(points[1]), radius, 10);
}
}
function BuildArchimedeanSpiral(shape, center, spacing, angleOffset, radiusMax, chordDegrees)
{
var fig = shape.GetFigure();
fig.BeginShape();
angleOffset = angleOffset / 180 * Math.PI;
var A = spacing / (Math.PI * 2);
var dtheta = (chordDegrees / 180 * Math.PI);
for (var theta = 0; ; theta += dtheta)
{
var r = A * theta;
var point = new DlxPoint();
point.x = (r * Math.cos(theta + angleOffset)) + center.x;
point.y = (r * Math.sin(theta + angleOffset)) + center.y;
fig.AddPoint(point);
if (r > radiusMax)
break;
}
fig.EndShape(false);
}
function DrawSpiral()
{
var layer = DlxApp.GetActiveLayer();
if ((layer.IsValid()) && (layer.GetType() == DlxApp.LAYERTYPE_DRAWING))
{
var page = layer.GetPage();
var doc = layer.GetDocument();
// Clear current selection.
var selection = page.GetSelection();
selection.Empty();
// Activate the tool to capture the center of the spiral and the distance between loops.
var centerAndSpacing = doc.ToolPickPoint(0, 2);
if ((centerAndSpacing.IsValid()) && (centerAndSpacing.Length() >= 0.1))
{
// Create a DlxShape object.
var shape = layer.DrawShape();
// Turn off undo registrations. This prevents changes to the shape object
// from being recorded in the next step of acquiring the spiral radius.
page.EnableUndo(false, false);
// Saves the useful data for the subsequent phase of acquiring the spiral radius.
DlxVariables.SetVar("shape", shape);
DlxVariables.SetVar("centerAndSpacing", centerAndSpacing);
// Activate the tool to acquire the spiral radius.
// In this phase, the OnToolPickPoint function is called at each mouse movement.
var radius = doc.ToolPickPoint(1, 2, DlxApp.TOOLPICKPOINT_SHOWLAST, "", centerAndSpacing.p1);
if ((radius.IsValid()) && (radius.Length() >= 0.1))
{
// The data is valid. Draw the spiral and select it.
BuildArchimedeanSpiral(shape, centerAndSpacing.p1, centerAndSpacing.Length(), radius.ArcAngle(), radius.Length(), 5);
selection.AddObject(shape);
}
else
{
// The operation was canceled or the data is invalid. Delete the DlxShape object.
selection.AddObject(shape);
selection.Delete();
}
// Turn on undo registrations.
page.EnableUndo(true);
}
}
}
|