Skip to main content
K.Daube
Community Expert
Community Expert
December 20, 2018
Question

Drawing Polyline finally solved

  • December 20, 2018
  • 0 replies
  • 584 views

Dear all

DrawPolyline is based on the DrawLine function: the segments are drawn as line, then joined by means of FCodes.

Next year I will follow Markus' advice and replace the flipping in DrawLine by rotation - but this requires some math - and is less transparent.

And yes, i will report these two scripts as bypass in various FM bug reports.

#target framemaker

Main();

function Main(oDoc) { // ==========================================================================

var oDoc = app.ActiveDoc, CM = 1857713,           // Constants.FV_Metric_CM not present in ESTK

    oFrame, oPoly1, rVertices = [4,1, 3,3, 5.5,3.6, 8,3, 6,2];      // values in cm

  oFrame = oDoc.FirstSelectedGraphicInDoc;

  if (!oFrame.ObjectValid()) {

    alert ("An anchored frame must be selected.");

    return;

  }

  oPoly0  = DrawPolyline(oDoc, oFrame, rVertices, false);  // unsmoothed line

  $.writeln ("location, dimension =" + oPoly0.LocX/CM + " " + oPoly0.LocY/CM + "   " + oPoly0.Width/CM + " " + oPoly0.Height/CM);

  oPoly1  = DrawPolyline(oDoc, oFrame, rVertices, true);  // smoothed line

  $.writeln ("location, dimension =" + oPoly1.LocX/CM + " " + oPoly1.LocY/CM + "   " + oPoly1.Width/CM + " " + oPoly1.Height/CM);

} //--- end Main

function DrawPolyline (oDoc, oFrame, rVertices, iSmooth) { // === Draw polygon line in FM-coordinates

// Arguments  oDoc:      Current document

//            oFrame:    Current (anchored|grphic) frame receiving the polyline

//            rVertices: Array of x, y values of the points forming the polyline [cm], FM-coordinates

//            iSmooth:   0: ragged ine; 1: smooth line (vertices are Bezier points).

// Returns    Created object, used e.g. for grouping

// Comment    Not yet supported (constants): lineWidth, colour, pen-pattern, dash-pattern, check arguments

//            x0, y0:    Origin of the object is deducted from the vertices.

// Attention  Due to severe misfunction of ESTK Polyline this routine is a polyfill: The vertices are

//            drawn as separate lines and then joined. This converts the selection to a Polyline.

var j, CM = 1857713, aFCodes = [], oLine, aLines = [], oPoly;

  jMax = rVertices.length/2 - 1;                  // number of lines (# points - 1)

  minX = minY = Number.MAX_VALUE;

  maxX = maxY = 0;                                // in units

  for (j = 0; j < jMax; j++) {

    oLine = CreateLine (oDoc, oFrame, rVertices[j*2], rVertices[j*2+1] , rVertices[j*2+2] , rVertices[j*2+3] );

    aLines.push(oLine);

  }

  for (j = 0; j < jMax; j++) {                    // select all lines

    aLines.GraphicIsSelected = 1;

  }

  aFCodes[0] = FCodes.KBD_JOINCURVES;             // there is no joining function in ESTK or FDK

  Fcodes(aFCodes);                                // join the lines (converts selection to polyline)

  oPoly = oDoc.FirstSelectedGraphicInDoc;         // this has all properties set (LocX/Y, W/H)

  oPoly.GraphicIsSelected = 0;                    // unselect after creation

  oPoly.Color = oDoc.GetNamedColor("Magenta");

  oPoly.BorderWidth = 0.05*CM;

  oPoly.PolyIsBezier = iSmooth;

  return oPoly;

} //--- end DrawPolyline

function DrawLine (oDoc, oFrame, x0, y0, x1, y1) { // Draw line in FM-coordinates =================

// Arguments  oDoc      current document

//            oFrame    parent object of the drawing object

//            x0/y0     coordinates in FM system of starting point [cm]

//            x1/y1     coordinates in FM system of end point [cm]

// Returns    Line object (for grouping etc.)

// Comment    The separation into quadrants is not completely to my intentions:

//            6/4, 6/1 goes to Q-III, not Q-IV as i intended in coherence

//            to the other 90° items at the 'beginning' of the quadrants

// Attention  The object Line has various flaws, which need strange bypasses

var CM = 1857713, oLine, width, height, aFCodes = [], tolerance = 0.001

    FRMAKER5819 = 8192;                           // Units to cope with bug FRMAKER-5819

  aFCodes[0] = FCodes.KBD_FLIPUD;                 // there is no flipping function in ESTK or FDK

  oLine = oDoc.NewLine(oFrame);

  width  = x1 - x0;

  height = y1 - y0;

  oLine.Width  = Math.abs(width)* CM;

  oLine.Height = Math.abs(height)* CM;

  if (Math.abs(width)* CM < FRMAKER5819)  {       // bypasses for FRMAKER-5819

    oLine.Width   = FRMAKER5819};

  if (Math.abs(height)* CM < FRMAKER5819) {

    oLine.Height  = FRMAKER5819};

  if (x0 < x1 & y0 <= y1) { quadrant = "  I";

    oLine.LocX = x0 * CM; oLine.LocY = y0 * CM;

  } else if (x0 >= x1  & y0 < y1) { quadrant = " II";

    oLine.LocX = x1 * CM; oLine.LocY = y0 * CM;

    oLine.GraphicIsSelected = 1;                  // bypass for FRMAKER-5844

    Fcodes(aFCodes);

    oLine.GraphicIsSelected = 0;

  } else if (x0 > x1  & y0 >= y1)  { quadrant = "III";

    oLine.LocX = x1 * CM; oLine.LocY = y1 * CM;

  } else if (x0 <= x1 & y0 > y1)  { quadrant = " IV";

    oLine.LocX = x0 * CM; oLine.LocY = y1 * CM; 

    oLine.GraphicIsSelected = 1;                  // bypass for FRMAKER-5844

    Fcodes(aFCodes);

    oLine.GraphicIsSelected = 0;

  } else {

    ZStop ("Program error in DrawLine");

  }

  oLine.BorderWidth = 0.05*CM;                    // Stroke-width

  oLine.Color = oDoc.GetNamedColor("Magenta");

//$.writeln ("Quadrant, x0,y01, x10,y1 = " + quadrant + "   " + x0 + " " + y0 + "   " + x1 + " " + y1);

  return oLine;

} //--- end DrawLine

function ZStop (message) { // =====================================================================

  throw new Error("Stop Script --- " + message);

}

Merry Christmas and a Happy New Year to all of You!

Klaus

This topic has been closed for replies.