Author Topic: [XDrX-PlugIn(4)] Draw the center lines of two SPLINEs  (Read 719 times)

0 Members and 1 Guest are viewing this topic.

xdcad

  • Bull Frog
  • Posts: 424
[XDrX-PlugIn(4)] Draw the center lines of two SPLINEs
« on: November 25, 2023, 05:34:43 AM »
https://www.keanw.com/2013/07/finding-an-autocad-spline-between-two-others-using-net.html

https://www.theswamp.org/index.php?topic=58736.0

How to determine the NurbCurve3d center between two NurbCurve3ds?

Kean Walmsley:

I chose to interpret this in the following way: given two NurbCurve3d objects, create a NurbCurve3d that sits exactly between the two. NurbCurve3d are “AcGe” classes – which means they’re non-graphical – so I’ve broadened the scope to deal with Splines as they have a close relationship with the NurbCurve3d class (in fact there are handy methods to convert between the two classes). Here’s some more information on NURBS, for those with an interest.

I’ve also chosen to deal with one fairly specific case: one where the Splines (and the equivalent NurbCurve3ds) have exactly the same degree and period as well as the same number of control points, knots and weights. Which makes things much easier: while it should be possible – with enough effort and code – to find a way to parameterize the two curves and create points on a curve between the two – fitting a curve along those points – I’ve chosen not to address that more general problem. At least not in this post.

So what I’ve done is actually very simple: between two similarly-sized NurbCurve3d objects, we step through the three primary collections of each – control points, knots and weights – and create three new collections for the new curve that contain the “average” values of the data from the two source objects. I’m far from being a NURBS expert, but I believe this approach to be reasonable for this particular category of Spline (at least it seems to work well enough).

Bear in mind that this also won’t work with Splines that are defined by fit – rather than control – points. At least I don’t expect it to. :-)


Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ()
  2.   (defun _check-curve (spl1 spl2)
  3.     (and (= (xdrx-getpropertyvalue g1 "degree")
  4.             (xdrx-getpropertyvalue g2 "degree")
  5.          )
  6.          (= (xdrx-getpropertyvalue g1 "isPeriodic")
  7.             (xdrx-getpropertyvalue g2 "isPeriodic")
  8.          )
  9.          (= (xdrx-getpropertyvalue g1 "numcontrolpoints")
  10.             (xdrx-getpropertyvalue g2 "numcontrolpoints")
  11.          )
  12.          (= (xdrx-getpropertyvalue g1 "numKnots")
  13.             (xdrx-getpropertyvalue g2 "numKnots")
  14.          )
  15.          (= (xdrx-getpropertyvalue g1 "numWeights")
  16.             (xdrx-getpropertyvalue g2 "numWeights")
  17.          )
  18.     )
  19.   )
  20.   (if (and (setq e1
  21.                   (car
  22.                     (xdrx-entsel
  23.                       "\nSelect the first SPLINE <exit>:"
  24.                       '((0 . "spline"))
  25.                     )
  26.                   )
  27.            )
  28.            (setq e2
  29.                   (car
  30.                     (xdrx-entsel
  31.                       "\nSelect the second SPLINE <exit>:"
  32.                       '((0 . "spline"))
  33.                     )
  34.                   )
  35.            )
  36.            (setq g1 (xdge::constructor e1))
  37.            (setq g2 (xdge::constructor e2))
  38.            (_check-curve e1 e2)
  39.       )
  40.     (progn
  41.       (xdrx-begin)
  42.       (setq degree         (xdrx-getpropertyvalue g1 "degree")
  43.                period         (xdrx-getpropertyvalue g1 "isPeriodic")
  44.                pts1            (xdrx-getpropertyvalue g1 "controlpoints")
  45.                pts2            (xdrx-getpropertyvalue g2 "controlpoints")
  46.                knots1         (xdrx-getpropertyvalue g1 "knots")
  47.                knots2         (xdrx-getpropertyvalue g2 "knots")
  48.                numKnots    (xdrx-getpropertyvalue g1 "numKnots")
  49.                numWeights (xdrx-getpropertyvalue g1 "numWeights")
  50.       )
  51.       (setq pts (mapcar '(lambda (x y)
  52.                            (xdrx-line-midp x y)
  53.                          )
  54.                         pts1
  55.                         pts2
  56.                 )
  57.       )
  58.       (setq knots (mapcar '(lambda (x y)
  59.                              (/ (+ x y) 2.0)
  60.                            )
  61.                           knots1
  62.                           knots2
  63.                   )
  64.       )
  65.       (setq i 0)
  66.       (repeat numWeights
  67.         (setq knots (append
  68.                       knots
  69.                       (list (/ (+ (xdrx-getpropertyvalue g1 "weightat" i)
  70.                                   (xdrx-getpropertyvalue g2 "weightat" i)
  71.                                )
  72.                                2.0
  73.                             )
  74.                       )
  75.                     )
  76.               i     (1+i)
  77.         )
  78.       )
  79.       (setq gNurb (xdge::constructor
  80.                     "kNurbCurve3d" degree knots pts period)
  81.       )
  82.       (xdrx-entity-make gNurb)
  83.       (xdrx-end)
  84.     )
  85.   )
  86.   (princ)
  87. )
  88.  
« Last Edit: November 25, 2023, 05:54:24 AM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
[XDrx-Sub Forum]
https://www.theswamp.org/index.php?board=78.0
https://github.com/xdcad/XDrx-API
http://bbs.xdcad.net