# Thread: How to create boundary box with spline entity?

1. ## How to create boundary box with spline entity?

Registered forum members do not see this ad.

Hi pro guru.
Anybody can help me how to write a lisp can create a boundary box with spline entity?

2. Not sure what you mean by "boundary box" - if is about "bounding box", then please check this routine written by Lee Mac.

3. Yes I mean like the routine written by Lee Mac. But this Lisp cannot correctly make the bounding box for the spline. I need is really close to the spline ==> "mean no gap".

4. Indeed, the BoundingBox method is inaccurate for Splines.

An alternative approach might be to approximate the Spline using an appropriate point list, then calculate the bounds of such point list; though, this algorithm will undoubtedly be slower than the BoundingBox method.

Here is an example to demonstrate this method (you will need to download my Entity to Point List function):

Code:
```(defun BoundingBox ( ent / l )
(if (setq l (LM:Entity->PointList ent))
(mapcar '(lambda ( a ) (apply 'mapcar (cons a l))) '(min max))
)
)```

5. Alternatively may create a copy in place of the spline, apply FLATTEN command on it and use Lee’s routine for bounding box; remove helper polyline just after.

6. Nice idea Mircea!

Here is another method (excuse the messy code):

Code:
```(defun SplineBoundingBox ( ent / pt1 pt2 res var )
(if (setq ent (entmakex (entget ent)))
(progn
(setq var (mapcar 'getvar '(peditaccept cmdecho)))
(mapcar 'setvar '(peditaccept cmdecho) '(1 0))
(command "_.pedit" ent 10 "")
(mapcar 'setvar '(peditaccept cmdecho) var)
(if (not (equal ent (setq ent (entlast))))
(progn
(vla-getboundingbox (vlax-ename->vla-object ent) 'pt1 'pt2)
(setq res (mapcar 'vlax-safearray->list (list pt1 pt2)))
)
)
(entdel ent)
)
)
res
)```
Test function:
Code:
```(defun c:test ( / bb en )
(princ "\nSelect a Spline: ")
(if (setq en (ssget "_+.:E:S:L" '((0 . "SPLINE"))))
(if (setq bb (SplineBoundingBox (ssname en 0)))
(entmakex
(list
(cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 4)
(cons 70 1)
(list 10 (caar  bb) (cadar  bb))
)
)
(princ "\nUnable to retrieve Bounding Box.")
)
)
(princ)
)

7. I think that Lee's code won't be correct for earlier versions of ACAD... If you don't need exactly but approx result, try this...

Code:
```(defun SplineBoundingBox ( ent / xmin xmax ymin ymax res cmde )
(setq cmde (getvar 'cmdecho))
(setvar 'cmdecho 0)
(vl-cmdf "_.ucs" "w")
(vl-cmdf "_.plan" "")
(vl-cmdf "_.zoom" "o" ent "")
(setq ymin (- (cadr (getvar 'viewctr)) (/ (getvar 'viewsize) 2.0)) ymax (+ (cadr (getvar 'viewctr)) (/ (getvar 'viewsize) 2.0)))
(vl-cmdf "_.ucs" "z" 90)
(vl-cmdf "_.plan" "")
(vl-cmdf "_.zoom" "o" ent "")
(setq xmax (- (car (trans (getvar 'viewctr) 1 0)) (/ (getvar 'viewsize) 2.0)) xmin (+ (car (trans (getvar 'viewctr) 1 0)) (/ (getvar 'viewsize) 2.0)))
(vl-cmdf "_.zoom" "p")
(vl-cmdf "_.zoom" "p")
(vl-cmdf "_.zoom" "p")
(vl-cmdf "_.zoom" "p")
(vl-cmdf "_.zoom" "p")
(vl-cmdf "_.ucs" "p")
(vl-cmdf "_.ucs" "p")
(setvar 'cmdecho cmde)
(setq res (list (list xmin ymin 0.0) (list xmax ymax 0.0)))
res
)

(defun c:test ( / bb en )
(princ "\nSelect a Spline: ")
(if (setq en (ssget "_+.:E:S:L" '((0 . "SPLINE"))))
(if (setq bb (SplineBoundingBox (ssname en 0)))
(entmakex
(list
(cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 4)
(cons 70 1)
(list 10 (caar  bb) (cadar  bb))
)
)
(princ "\nUnable to retrieve Bounding Box.")
)
)
(princ)
)```
If you want exact results, consider converting SPLINE to POLYLINE like Mircea suggested or if you have A2010 or above (not sure for A2009) A2008 doesn't support - try SPLINEDIT -> Convert to polyline and obtain Bounding Box from pline - after remove dummy pline... If you have A2010 or above, then try also Lee's code - just checked on my A2012 and it worked...
Also check this thread from theswamp...

M.R.

8. Thank for Lee Mac and Marko Ribar Lisp. After try on my AutoCAD 2007, still cannot work well. Maybe my AutoCAD are version are too old. Both of your code cannot support. Anyway thank you both of you. I still need to find out how to break though it.

9. Please check this solution to convert the said spline to a polyline using your AutoCAD version.

10. Registered forum members do not see this ad.

Originally Posted by kwwong6
Thank for Lee Mac and Marko Ribar Lisp. After try on my AutoCAD 2007, still cannot work well. Maybe my AutoCAD are version are too old. Both of your code cannot support. Anyway thank you both of you. I still need to find out how to break though it.
kwwong6,

As far as I know, the solution that I posted in reply#4 should work in AutoCAD 2007.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts