【问题标题】:How to convert an LSP into objectarx C# to close Autocad polylines?如何将 LSP 转换为 objectarx C# 以关闭 Autocad 多段线?
【发布时间】:2019-07-08 15:49:32
【问题描述】:

我需要创建一个 C# objectarx 函数来检查折线是否已连接并打开。如果是这样,则关闭它们的折线。此示例代码无法使用,因为它不会检查它是否已连接:

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

我发现了如何做到这一点,但它在 LISP 中。有谁知道如何将其转换为 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

更新 #1

我实际上想要做的是确定是否应该关闭折线。想象一条形状为字母 C 的折线和另一条形状为字母 O 的折线。在这种情况下,我想关闭形状为字母 O 的折线。

例子:

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

}

【问题讨论】:

  • 专业提示:如果您对某个问题投了一两票,请尽量忽略它们,或者发表评论寻求反馈。 99% 的此类查询都没有得到答复,因为投反对票的人早就离开了,所以我建议你只在广泛的问题和答案上获得代表,这样投反对票对你来说就不再重要了。投票建议不属于问题,因为它与手头的问题无关。
  • 能否提供测试图?

标签: autocad autocad-plugin objectarx


【解决方案1】:

如果将多段线投射为曲线对象,则可以检查起点和终点是否相同。

将多段线投射为曲线可以省去处理不同多段线类型的麻烦。

【讨论】:

  • Miir,我正在尝试将上述代码从 LSP 转换为 C# (ObjectArx)。如果您有有关如何执行此操作的实际代码,请告诉我!感谢您的贡献。
【解决方案2】:

你能提供一张测试图吗? 而且,如果你真的想使用 lisp 中的逻辑,你可以随时从 .NET 中调用 LISP 函数。

例如你有 lisp 函数

;(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,非常好的帖子!但我实际上想将代码转换为 C# 而不是调用 LISP 代码。谢谢。
猜你喜欢
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
  • 2010-10-18
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多