Drafting Script Example
Section Markers are custom drafting objects that allow a special set of 2D objects to be managed via a group whose group type is "SectionMarkers". The following example script demonstrates how we can use DW external functions to insert such markers into the active drawing view. After starting the script, the user can set up the look of the markers (color, pen, text height) and insert as many markers as necessary with the same settings. For each section marker, the user can specify which character to display next to the section marker arrows, draw the marker by picking the start point and the end point, and swap the direction of the arrows if needed.
The example script can be found in the following location:
%PMS_RUNDIR%\dw\macro\Examples\SectionMarkerTool.mac
Alternatively, you can copy the script code below and save it in a new .mac file.
/*
** @(#)SectionMarker.mac 1.2 96/05/09
**
**
** This script draws a section marker:
** Two arrows with a small piece of dotted dashed line, plus
** the marker characters. The label direction is oriented "reasonably" when
** the section is not parallel to x or y.
*/
#include "include/dmutil.h"
#include "include/dbase.h"
#include "include/dw.h"
#include "include/win.h"
#include "include/win_panel.h"
global int Color = 7;
global int Pen = 7;
global int Dir_stat;
global float TextHeight = 5.0;
global float ArrowLength = 20.0;
global float ArrowHeadLength = 7.0;
global float TextDistance = 18.0;
global string Gname = "";
global string MarkerStr = "";
global handle Group_h;
global float X1,X2,Y1,Y2; /* the strategic coordinate points */
global float Scale = 1.0;
createFrame(p)
{
tool = W_INIT_FRAME();
W_REALIZE_WINDOW(tool, W_FRAME_TITLE, p);
return(tool);
}
SetupSectionMarker(item, event_type, button_value)
{
col = 1; pen = 1; th = 5.0;
get_sectm_defs(col,pen,th);
U_INIT_DIALOG("Setup Section Markers");
i_col = U_SET_INT("Color",col,1,255);
i_pen = U_SET_INT("Pen ",pen,1,15);
i_th = U_SET_FLOAT("Text Height",th,1.0,100.0);
if( U_DIALOG() < 0 )return(-1);
col = U_GET_INT(i_col);
pen = U_GET_INT(i_pen);
th = U_GET_FLOAT(i_th);
module = "DwSectMark";
SET_INT_DEFAULT(module, "Color", col);
SET_INT_DEFAULT(module, "Pen", pen);
SET_FLOAT_DEFAULT(module, "TextH", th);
return(0);
}
CreateMainWindow()
{
tool = createFrame("Section Marker Tool");
panel = W_ADD_WINDOW(tool, W_PANEL, "menu_window");
W_REALIZE_WINDOW(panel, W_FRAME_X, 0,
W_FRAME_Y, 0,
W_FRAME_ROWS, 4,
W_FRAME_COLS, 20);
/* Create button */
sm_create = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Create");
W_REALIZE_PANELITEM(sm_create, W_PANEL_ROW, 0, W_PANEL_COL, 0,
W_PANEL_BUTTONIMAGE, "Create", W_PANEL_WIDTH, 6,
W_PANEL_BUTTONVALUE, 200,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_create, "CreateSectionMarkers");
/* Delete button */
sm_delete = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Delete");
W_REALIZE_PANELITEM(sm_delete, W_PANEL_ROW, 0, W_PANEL_COL, 8,
W_PANEL_BUTTONIMAGE, "Delete", W_PANEL_WIDTH, 6,
W_PANEL_BUTTONVALUE, 201,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_delete, "DeleteSectionMarkers");
/* Setup button */
sm_setup = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Setup");
W_REALIZE_PANELITEM(sm_setup, W_PANEL_ROW, 0, W_PANEL_COL, 16,
W_PANEL_BUTTONIMAGE, "Setup", W_PANEL_WIDTH, 6,
W_PANEL_BUTTONVALUE, 202,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_setup, "SetupSectionMarker");
/* Done button */
sm_done = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Done");
W_REALIZE_PANELITEM(sm_done, W_PANEL_ROW, 2, W_PANEL_COL, 4,
W_PANEL_BUTTONIMAGE, "Done", W_PANEL_WIDTH, 20,
W_PANEL_BUTTONVALUE, 203,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_done, "sm_done_hlr");
return tool;
}
CreateSectionMarkers(item, event_type, button_value)
{
cont = 1;
col = 1; pen = 1; th = 5.0;
get_sectm_defs(col,pen,th);
set_sectm_defs(col,pen,th);
cont = 1;
failed = 0;
while( cont ){
failed = 0;
U_INIT_DIALOG("Section Marker");
MarkerStr = "";
i_m = U_SET_STR("Marker Character",MarkerStr,20);
if( U_DIALOG() < 0 )return(0);
MarkerStr = U_GET_STR(i_m);
if( MarkerStr == "" ){
U_MESSAGE("Empty string not accepted for section marker !");
failed = 1;
}
/* test if group already exists */
if( ! failed ){
Gname = MarkerStr+"-"+MarkerStr;
g = DW_NEWGROUP("SectionMarker",Gname,0);
if( ISINT(g) ){
U_MESSAGE("Cannot create 2D group: <"+Gname+">. Name already in use ?");
failed = 1;
}
else{
DW_DELGROUP_ANDMEM( g );
}
}
if( ! failed ) {
X1 = 0.0; Y1 = 0.0;
X2 = 0.0; Y2 = 0.0;
dx = 0.0; dy = 0.0;
st = get_points(X1,Y1,X2,Y2);
if( st < 0 ){
return(0);
}
Group_h = DW_NEWGROUP("SectionMarker",Gname,0);
draw_sect_marker(MarkerStr,X1,Y1,X2,Y2,1,Color);
sectm_ok();
}
}
return(0);
}
createPanel(tool)
{
Dir_stat = 1;
panel = W_ADD_WINDOW(tool, W_PANEL, "sm_pan");
W_REALIZE_WINDOW(panel, W_FRAME_X, 0,
W_FRAME_Y, 0,
W_FRAME_ROWS, 4,
W_FRAME_COLS, 20);
/* Done button */
sm_done = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Done");
W_REALIZE_PANELITEM(sm_done, W_PANEL_ROW, 0, W_PANEL_COL, 0,
W_PANEL_BUTTONIMAGE, "Done", W_PANEL_WIDTH, 6,
W_PANEL_BUTTONVALUE, 200,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_done, "sm_done_hlr");
/* Cancel button */
sm_cancel = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Cancel");
W_REALIZE_PANELITEM(sm_cancel, W_PANEL_ROW, 0, W_PANEL_COL, 16,
W_PANEL_BUTTONIMAGE, "Cancel", W_PANEL_WIDTH, 6,
W_PANEL_BUTTONVALUE, 201,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_cancel, "sm_canc_hlr");
/* Swap direction button */
sm_dir = W_ADD_PANELITEM(panel, W_PANEL_BUTTON, "Dir");
W_REALIZE_PANELITEM(sm_dir, W_PANEL_ROW, 2, W_PANEL_COL, 3,
W_PANEL_BUTTONIMAGE, "Swap Dir", W_PANEL_WIDTH, 14,
W_PANEL_BUTTONVALUE, 202,
W_PANEL_CALLBACK, 1,
W_PANEL_LAYOUT, W_LAYOUT_CENTER);
W_SET_PANELITEM_HANDLER(sm_dir, "sm_dir_hlr");
}
sm_dir_hlr(item, event_type, button_value)
{
if(Dir_stat == 1) Dir_stat = -1;
else
Dir_stat = 1;
DW_DELGROUP_ANDMEM( Group_h );
Group_h = DW_NEWGROUP("SectionMarker",Gname,0);
draw_sect_marker(MarkerStr,X1,Y1,X2,Y2,Dir_stat,Color);
return(0);
}
sm_done_hlr(item, event_type, button_value)
{
return(99);
}
sm_canc_hlr(item, event_type, button_value)
{
DW_DELGROUP_ANDMEM( Group_h );
DW_REDRAW();
Dir_stat = 0;
return(99);
}
sectm_ok()
{
tool = createFrame("Section Marker");
panel = createPanel(tool);
W_MAP_FRAME(tool);
W_RUN_FRAME(tool);
W_UNMAP_FRAME(tool);
W_DESTROY_FRAME(tool);
return(Dir_stat);
}
DeleteSectionMarkers(item, event_type, button_value)
{
st = 0;
g = DW_SELECTGROUP( "SectionMarker", "Pick the section marker to be deleted",st);
if( st == 1 ){
U_MESSAGE("NO Section Markers in the View !");
}
if( ! ISINT(g) ){
DW_DELGROUP_ANDMEM(g);
DW_REDRAW();
}
return(0);
}
get_sectm_defs(col,pen,th)
{
module = "DwSectMark";
if (GET_INT_DEFAULT(module, "Color", col) != 0)
col = 7;
if (GET_INT_DEFAULT(module, "Pen", pen) != 0)
pen = 4;
if (GET_FLOAT_DEFAULT(module, "TextH", th) != 0)
th = 5.0;
}
set_sectm_defs(col,pen,th)
{
xmin=0.0; ymin=0.0; zmin=0.0; xmax=0.0; ymax=0.0; zmax=0.0; scale =1.0;
DW_GET_VIEW_PARAMS(xmin, ymin, zmin, xmax, ymax, zmax, scale);
Color = col;
Pen = pen;
TextHeight = th;
ArrowLength = 20.0/5.0*th;
ArrowHeadLength = 7.0/5.0*th;
TextDistance = 18.0/5.0*th;
Scale = scale;
}
get_points(x1,y1,x2,y2)
{
st = DW_QPOINT("Left end of the section marker",x1,y1);
if(st < 0)return(-1);
DW_SETQREFPNT(x1,y1);
DW_SETQMODE("RUBBERBAND");
st = DW_QPOINT("Right end of the section marker",x2,y2);
DW_SETQMODE("NORMAL");
if(st < 0)return(-1);
return(0);
}
get_dir(x1,y1,x2,y2,dx,dy)
{
d1 = ATAN2(y2-y1,x2-x1);
dx = COS(d1);
dy= SIN(d1);
return(d1);
}
/* Draw the sect marker
** All the params must be multiplied with scale since in
** PM views the units are in world coordinates - not in sheet
** coordinates
*/
draw_sect_marker(sc,x1,y1,x2,y2,a_dir,color)
{
DW_COLOR(color);
DW_LTYPE(5,14.0*Scale);
DW_PEN(Pen);
dx =0; dy = 0;
d = get_dir(x1,y1,x2,y2,dx,dy);
/* offset of the line segments */
/*
** S !-section arrow
** !
** V
** ============ = -section line
**
** !- a -!--- b ---! -params a and b
*/
a = 3.0*Scale;
b= 15.0*Scale;
xx1 = x1-a*dx;
yy1 = y1-a*dy;
xx2 = x1+b*dx;
yy2 = y1+b*dy;
Poly1 = DW_POLY(xx1,yy1,xx2,yy2);
xx1 = x2+a*dx;
yy1 = y2+a*dy;
xx2 = x2-b*dx;
yy2 = y2-b*dy;
Poly2 = DW_POLY(xx1,yy1,xx2,yy2);
DW_LTYPE(1,3.0*Scale);
r = d-a_dir*90.0;
DW_SYMTRF(Scale,Scale,r);
if(a_dir == 1){
s1 = "sect_left";
s2 = "sect_right";
}
else{
s1 = "sect_right";
s2 = "sect_left";
}
Sym1 = DW_SYMBOL(s1,x1,y1,ArrowLength, ArrowHeadLength);
Sym2 = DW_SYMBOL(s2,x2,y2,ArrowLength, ArrowHeadLength);
if( d <= 1.0 & d > -179.0 )
td = d+90.0;
else
td = d-90.0;
DW_TATTR(td,0.0,5);
DW_TSIZE(TextHeight*Scale);
B = d-a_dir*120.0;
xx1 = x1+TextDistance*COS(B)*Scale;
yy1 = y1+TextDistance*SIN(B)*Scale;
Txt1 = DW_TEXT(sc,xx1,yy1);
B = d-a_dir*60.0;
xx1 = x2+TextDistance*COS(B)*Scale;
yy1 = y2+TextDistance*SIN(B)*Scale;
Txt2 = DW_TEXT(sc,xx1,yy1);
DW_ADDGROUPMEM(Group_h, 6, Poly1, Poly2, Sym1, Sym2, Txt1, Txt2);
DW_REDRAW();
return(0);
}
main()
{
tool = CreateMainWindow();
W_MAP_FRAME(tool);
W_RUN_FRAME(tool);
W_UNMAP_FRAME(tool);
W_DESTROY_FRAME(tool);
}
To run the script, open a document for editing, select a drawing view from Drafting > Settings > Annotating, and run the .mac file from Import > Run > Drafting Script. As a result, the Section Marker Tool dialog opens, and you can use the following commands.
Create
The Create button first prompts you to specify the character to display next to the two section marker arrows.
When you click OK, you are prompted to pick the start point and the end point of the section marker. The section marker is drawn using the properties defined in the setup, and then the following dialog opens:
-
Done – Completes the current section marker and starts the insertion of another one.
-
Cancel – Cancels the current section marker and starts the insertion of another one.
-
Swap Dir – Swaps the direction of the marker arrows.
Delete
The Delete button opens a list of existing section markers. Select a section marker from the list to delete both the group entity and the 2D objects in that group.
Setup
The Setup button opens a dialog for defining the properties of new section markers:
- Color
- Pen
- Text height
Done
The Done button closes the section marker tool.