-1

I need to create a C# objectarx function that will check if a polyline is connected and open. If so then close they polyline. This sample code cannot be used because this will not check if it is connected:

if (polyline.Closed == false)
{
    // Close polyline
    polyline.Closed = true;
}

I found out how to do it but it is in LISP. Does anyone know how to convert this to C# objectarx .net?

;;  PLsCloseCorners.lsp [command name: PLsCL for PolyLines CLose]
;;  To Close all open lightweight Polylines, with the start/end
;;  vertex at the [apparent] intersection of the starting and
;;  ending segments, without coincident start/end vertices.
;;  If one "looks" closed (i.e. last vertex coincides with first one),
;;  but is not closed in Polyline terms, this will close it from
;;  the next-to-last vertex, not by adding a zero-length segment.
;;  [If beginning and/or ending Polyline segment is/are arcs, and
;;  start/end vertices are not coincident, will locate new corner
;;  as if endpoints of arc(s) are endpoints of line segment(s);
;;  if ending segment is an arc and start/end vertices are not
;;  coincident, will alter arc's path.]
;;  Kent Cooper, July 2009
;;
(defun C:PLsCL (/ plset pl plverts corner)
  (setq cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (command "_.undo" "_begin")
  (setq plset (ssget "X" '((0 . "LWPOLYLINE"))))
    ; omit the "X" from the above line to let User select them
  (while (> (sslength plset) 0)
    (setq pl (ssname plset 0))
    (if (not (vlax-curve-isclosed pl))
      (progn
        (setq
          plverts (cdr (assoc 90 (entget pl))); number of vertices
          corner
            (inters 
              (vlax-curve-getStartPoint pl)
              (vlax-curve-getPointAtParam pl 1)
              (vlax-curve-getPointAtParam pl (1- plverts))
              (vlax-curve-getPointAtParam pl (- plverts 2))
              nil
            ); end inters & corner
        ); end setq
        (command
          "_.pedit"
          pl
          "_edit"
          "_move"
          corner
        ); end command
        (repeat (- plverts 2)
          (command "_next"); move to next-to-last vertex
        ); end repeat
        (command
          "_break"
          "_next"
          "_go"
          "_eXit"
          "_close"
          ""
        ); end command
      ); end progn
    ); end if
    (ssdel (ssname plset 0) plset)
  ); end while
  (command "_.undo" "_end")
  (setvar 'cmdecho cmde)
  (princ)
); end defun

Update #1

What I am actually trying to do is determine if a polyline should be closed. Picture a polyline shaped as the letter C and another polyline as the letter O. In this case, I would like to close the one shaped as the letter O.

Example:

public bool IsPolylineConnected(Polyline pline)
{

  // Convert the code from the LSP to C#
  // A polyline with the shape of the letter C would return false
  // A polyline with the shape of the letter O would return true

}
halfer
  • 19,824
  • 17
  • 99
  • 186
Robert Smith
  • 634
  • 1
  • 8
  • 22
  • Pro tip: if you get one or two downvotes on a question, ignore them if you can, or post a _comment_ asking for feedback. 99% of such queries go unanswered, since the downvoter has long since gone, so I would advise you just to acquire rep on a broad set of questions and answers, so that downvotes no longer matter to you. Voting advice does not belong in questions, since it is not germane to the problem at hand. – halfer Feb 17 '19 at 11:18
  • Can you provide a test drawing ? – Madhukar Moogala Feb 18 '19 at 05:36

2 Answers2

0

If you cast the polyline as a curve object you can check if the start point and end point are the same.

Casting the polyline as a curve saves you the trouble of dealing with the different polyline types.

Miiir
  • 731
  • 1
  • 9
  • 12
  • Miir, I am trying to convert the above code from LSP to C# (ObjectArx). If you have the actual code on how to do this please let me know! Thank you for your contribution. – Robert Smith Feb 14 '19 at 16:27
0

Can you provide a test drawing ? And, if you really want to use the logic in lisp, you can always call LISP function from .NET.

For example you have lisp function

;(defun DoIt()

;Define the Lisp function as a command using c:
(defun c:DoIt()

    (setq    pntA (getpoint "\nPick A")
            pntB (getpoint pntA "\nPick B")
    )
    (grdraw pntA pntB 1 2)

)

C#:

[CommandMethod("DoIt", CommandFlags.Session)]
public void DoItMethod()
{
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document acDoc = acDocMgr.Open(@"C:\SampleDrawings\Lights.dwg", false);
using (DocumentLock dl = acDoc.LockDocument())
{
Editor ed = acDoc.Editor;
using (ResultBuffer rb = new ResultBuffer())
{
rb.Add(new TypedValue((int)LispDataType.Text, "c:DoIt"));
ResultBuffer rbRes = Application.Invoke(rb);
if (rbRes != null)
{
TypedValue[] tvalues = rbRes.AsArray();
foreach (TypedValue tv in tvalues)
ed.WriteMessage("\n" + tv.ToString());
rbRes.Dispose();
}
else
ed.WriteMessage("\n Result buffer is null.");
}
}
}
Madhukar Moogala
  • 635
  • 1
  • 5
  • 11
  • Madhukar, very nice post! But I would actually like to convert the code to C# instead of calling LISP code. Thank you. – Robert Smith Feb 19 '19 at 19:56