Author Topic: bounding box along direction  (Read 5762 times)

0 Members and 1 Guest are viewing this topic.

domenicomaria

  • Swamp Rat
  • Posts: 743
bounding box along direction
« on: August 21, 2023, 03:12:38 AM »
I need to know the red size in the second attached image.
Without rotating anything !
Is it possible ?
« Last Edit: August 21, 2023, 03:20:36 AM by domenicomaria »


ribarm

  • Gator
  • Posts: 3328
  • Marko Ribar, architect
Re: bounding box along direction
« Reply #2 on: August 21, 2023, 04:44:33 AM »
Rotate UCS and apply this routine to get BBox, then extract only width (red line)...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1637
  • Ukraine
Re: bounding box along direction
« Reply #3 on: August 21, 2023, 05:07:31 AM »
build a list of coordinates (curves must be approximated)
then either rotate every point around the common center or project them on a red line
then find min and max

some helper functions https://www.theswamp.org/index.php?topic=37319.msg423645#msg423645

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #4 on: August 21, 2023, 05:19:33 AM »
Rotate UCS and apply this routine to get BBox, then extract only width (red line)...

Thank you Ribarm!
Works !
But I was hoping for a mathematical solution. . .
Because this routine does a number of very spectacular things,
while I'm looking for something that can't be seen,
that makes calculations "in secret" ...

But I realize that it is a very complicated thing!

I have to try to do something only for lines, circles and arcs...
It shouldn't be hard...

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #5 on: August 21, 2023, 05:20:52 AM »
build a list of coordinates (curves must be approximated)
then either rotate every point around the common center or project them on a red line
then find min and max

some helper functions https://www.theswamp.org/index.php?topic=37319.msg423645#msg423645

VovKa you have allways great ideas ...
I will give it a try !
Grazie.
Ciao

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8927
  • AKA Daniel
Re: bounding box along direction
« Reply #6 on: August 21, 2023, 06:39:03 AM »
If that’s a polyline, made to represent an object, you can store a direction in XData when it’s created.
Then, you can get the direction at a later time, even if it’s been rotated 

dexus

  • Bull Frog
  • Posts: 232
Re: bounding box along direction
« Reply #7 on: August 21, 2023, 09:23:51 AM »
I tried to make an example by projecting the lines onto the line and getting the min and max. (like VovKa suggested)
It uses the code from http://www.lee-mac.com/entitytopointlist.html to make the pointlist.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:bbang (/ pl lin pts)
  2.   (and
  3.     (setq pl (car (entsel "\nSelect polyline: ")))
  4.     (setq pts (LM:ent->pts pl 50.0)) ; Get point list
  5.     (setq line (car (entsel "\nSelect angle line: ")))
  6.     (setq line (entget line))
  7.     (setq line (list (cdr (assoc 10 line)) (cdr (assoc 11 line)))) ; Start and end point
  8.     (setq line (cons (mapcar '- (cadr line) (car line)) line)) ; Add relative point
  9.     (setq pts (mapcar (function (lambda (pt) (caddr (trans (mapcar '- pt (cadr line)) 0 (car line))))) pts)) ; get distances on line
  10.     (princ "\nDistance on line: ") (princ (- (apply 'max pts) (apply 'min pts))) ; Subtract shortest from longest to get the distance
  11.   )
  12.   (princ)
  13. )
« Last Edit: August 21, 2023, 09:55:43 AM by dexus »

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #8 on: August 21, 2023, 09:42:47 AM »
@Dexus

It works very well !

it's enough for what I need !

that's exactly what vovKa suggested !

it's not perfect,
(because it doesn't analyze curves but only segments),
but for what I have to do,
it's fine.

Thank you all.


domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #9 on: August 21, 2023, 09:54:03 AM »
@Dexus
. . . your modified version has a little problem . . .

dexus

  • Bull Frog
  • Posts: 232
Re: bounding box along direction
« Reply #10 on: August 21, 2023, 09:56:30 AM »
@Dexus
. . . your modified version has a little problem . . .
It was missing a bracket, should work now.  :-D

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: bounding box along direction
« Reply #11 on: August 21, 2023, 10:39:05 AM »
Please post your sample DWG. > Edit...
Code: [Select]
(defun c:test (/ ent line llp urp )
    (and
        (setq ent (car (entsel "\nSelect polyline: ")))
        (setq line (car (entsel "\nSelect angle line: ")))
        (progn
            (command "_.UCS" "_ENTITY" line)           
            (gc:UcsBoundingBox ent 'llp 'urp)
            (command "_.rectangle" "_non" llp "_non" urp)
            (command "_.UCS" "_P")           
        )
    )
    (princ)
)
;; gc:UcsBoundingBox by Gile
;; Returns the UCS coordinates of the object bounding box about current UCS
;;
;; Arguments
;; obj: an entity (ENAME or VLA-OBJCET)
;; _OutputMinPtSym: a quoted symbol (output)
;; _OutputMaxPtSym: a quoted symbol (output)


(defun gc:UcsBoundingBox ( obj _OutputMinPtSym _OutputMaxPtSym )
    (and (= (type obj) 'ename)
         (setq obj (vlax-ename->vla-object obj))
    )
    (vla-transformby obj (vlax-tmatrix (gc:TMatrixFromTo 1 0)))
    (vla-getboundingbox obj _OutputMinPtSym _OutputMaxPtSym)
    (vla-transformby obj (vlax-tmatrix (gc:TMatrixFromTo 0 1)))
    (set _OutputMinPtSym (vlax-safearray->list (eval _OutputMinPtSym)))
    (set _OutputMaxPtSym (vlax-safearray->list (eval _OutputMaxPtSym)))
)


;; gc:TMatrixFromTo by Gile
;; Returns the 4X4 transformation matrix from a coordinate system to an other one
;;
;; Arguments
;; from to: same arguments as for the 'trans' function


(defun gc:TMatrixFromTo ( from to )
    (append
        (mapcar
            (function
                (lambda   ( v o )
                    (append (trans v from to t) (list o))
                )
            )
           '(
                (1.0 0.0 0.0)
                (0.0 1.0 0.0)
                (0.0 0.0 1.0)
            )
            (trans '(0.0 0.0 0.0) to from)
        )
       '((0.0 0.0 0.0 1.0))
    )
)

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #12 on: August 21, 2023, 11:16:44 AM »


VovKa

  • Water Moccasin
  • Posts: 1637
  • Ukraine
Re: bounding box along direction
« Reply #14 on: August 21, 2023, 11:47:42 AM »
it's not perfect,
(because it doesn't analyze curves but only segments),
LM:ent->pts builds approximations for curves, so it's better than you think :)

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #15 on: August 21, 2023, 01:10:22 PM »
it's not perfect,
(because it doesn't analyze curves but only segments),
LM:ent->pts builds approximations for curves, so it's better than you think :)
... curves converted into segments ...
I have seen the point list that it makes...
and I have seen how It works (very well)
... however It Is an approximation ...
 however it Is more than enough, for what i need
« Last Edit: August 21, 2023, 02:09:53 PM by domenicomaria »


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: bounding box along direction
« Reply #17 on: August 21, 2023, 03:08:10 PM »
See replay #1 and #11...  :knuppel2: :whistling:

Stefan

  • Bull Frog
  • Posts: 320
  • The most I miss IRL is the Undo button
Re: bounding box along direction
« Reply #18 on: August 21, 2023, 05:21:35 PM »
Here is an exact solution for polylines.
It is a mathematical solution, no entity is changed at all.

Code - Auto/Visual Lisp: [Select]
  1. (defun _rot (p a)
  2.   (list
  3.     (- (* (car p) (cos a)) (* (cadr p) (sin a)))
  4.     (+ (* (car p) (sin a)) (* (cadr p) (cos a)))
  5.   )
  6. )
  7.  
  8. (defun _m2p (a b) (mapcar '(lambda (a b) (/ (+ a b) 2.0)) a b))
  9.  
  10. (defun _pline_rotated_bb (e u / a b c d m o p r s a1 a2)
  11.         b (vlax-curve-getendparam e)
  12.   )
  13.   (while (<= a b)
  14.           r (_rot p (- u))
  15.           a1 (if a1 (mapcar 'min a1 r) r)
  16.           a2 (if a2 (mapcar 'max a2 r) r)
  17.           s (vlax-curve-getsecondderiv e a)
  18.           d (distance '(0.0 0.0) s)
  19.     )
  20.     (if
  21.       (> d 0.0)
  22.       (progn
  23.         (setq o (polar
  24.                   (setq m (vlax-curve-getpointatparam e (+ a 0.5)))
  25.                   (angle m (_m2p p (vlax-curve-getpointatparam e (rem (1+ a) b))))
  26.                   d
  27.                 )
  28.         )
  29.         (foreach x '(0.0 0.5 1.0 1.5)
  30.           (setq p (polar o (+ u (* x pi)) d))
  31.           (if
  32.             (and
  33.               (setq c (vlax-curve-getparamatpoint e p))
  34.               (< a c (1+ a))
  35.             )
  36.             (setq r (_rot p (- u))
  37.                   a1 (mapcar 'min a1 r)
  38.                   a2 (mapcar 'max a2 r)
  39.             )
  40.           )
  41.         )
  42.       )
  43.     )
  44.     (setq a (1+ a))
  45.   )
  46.   (list
  47.     (_rot a1 u)
  48.     (_rot (list (car a2) (cadr a1)) u)
  49.     (_rot a2 u)
  50.     (_rot (list (car a1) (cadr a2)) u)
  51.   )
  52. )
  53.  
  54. (defun c:test ( / *error* e u bb)
  55. ;;;  (setq *error* (err))
  56.   (if
  57.     (and
  58.       (setq e (ssget "_+.:S" '((0 . "*POLYLINE"))))
  59.       (setq u (getangle "\nSpecify rotation angle: "))
  60.       (setq bb (_pline_rotated_bb (ssname e 0) u))
  61.     )
  62.     (entmakex
  63.       (append
  64.        '(
  65.           (0 . "LWPOLYLINE")
  66.           (100 . "AcDbEntity")
  67.           (100 . "AcDbPolyline")
  68.           (90 . 4)
  69.           (70 . 1)
  70.         )
  71.         (mapcar
  72.          '(lambda (p)
  73.             (cons 10 p)
  74.           )
  75.           bb
  76.         )
  77.       )
  78.     )
  79.   )
  80. ;;;  (*error* nil)
  81.   (princ)
  82. )

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #19 on: August 22, 2023, 02:41:45 AM »
See replay #1 and #11...  :knuppel2: :whistling:
Marco, I apologize.
I didn't really notice the answer #11 !  :whistling:

Bravo.
Very interesting !

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #20 on: August 22, 2023, 02:49:09 AM »
@DEXUS
Quote
Here is an exact solution for polylines.
It is a mathematical solution, no entity is changed at all.

It works perfectly !

you are very good !

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #21 on: August 22, 2023, 03:02:52 AM »
See replay #1 and #11...  :knuppel2: :whistling:
. . . and your code works also with line, circle, arc, ellipse . . .
. . . while fails with spline, insert . . .

dexus

  • Bull Frog
  • Posts: 232
Re: bounding box along direction
« Reply #22 on: August 22, 2023, 03:22:34 AM »
@DEXUS
Quote
Here is an exact solution for polylines.
It is a mathematical solution, no entity is changed at all.

It works perfectly !

you are very good !
I would love to take credit, but it should be @Stefan :wink:

ribarm

  • Gator
  • Posts: 3328
  • Marko Ribar, architect
Re: bounding box along direction
« Reply #23 on: August 22, 2023, 03:56:31 AM »
I think Stefan will fail if reference entity is INSERT...
I've included all that's possible in my BBox.lsp... It'll try even with nested inserts that overlap each other...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #24 on: August 22, 2023, 04:34:07 AM »
@ Dexus and Stefan !
Quote
I would love to take credit, but it should be @Stefan :wink:

I'm always very distracted !  :-)

Stefan, YOU you are very good !

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #25 on: August 22, 2023, 04:36:02 AM »
I think Stefan will fail if reference entity is INSERT...
I've included all that's possible in my BBox.lsp... It'll try even with nested inserts that overlap each other...

The code of Stefan is only for polylines ...

Stefan

  • Bull Frog
  • Posts: 320
  • The most I miss IRL is the Undo button
Re: bounding box along direction
« Reply #26 on: August 22, 2023, 04:39:39 AM »

Thanks. My wife would disagree :)
But she knows nothing about lisp

ribarm

  • Gator
  • Posts: 3328
  • Marko Ribar, architect
Re: bounding box along direction
« Reply #27 on: August 22, 2023, 05:01:33 AM »
I've just tried my BBox.lsp and it fails with INSERTs too... I've fixed it only not to exit with error and I think that it explodes blocks if it runs on them, so be careful with *.lsp... But for curves it should work well and in 3d UCS... HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

xdcad

  • Swamp Rat
  • Posts: 527
Re: bounding box along direction
« Reply #28 on: November 20, 2023, 11:02:38 PM »
I think Stefan will fail if reference entity is INSERT...
I've included all that's possible in my BBox.lsp... It'll try even with nested inserts that overlap each other...

The code of Stefan is only for polylines ...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt ()
  2.   (if (setq ss (xdrx-ssget "\nSelect Draw Boundingbox Objects<Quit>:"))
  3.     (progn
  4.       (while (setq e (xdrx-entsel
  5.                        "\nPick a Direction Line<Quit>:"
  6.                        '((0 . "*polyline,line"))
  7.                      )
  8.              )
  9.         (setq xdir (xdrx-getpropertyvalue (car e) "firstderiv" (cadr e))
  10.               box  (xdrx-entity-box ss xdir)
  11.         )
  12.         (xdrx-polyline-make (xdrx-points-ucs2wcs box) t)
  13.         (xdrx-setpropertyvalue (entlast) "color" (xdrx-math-rand 1 220))
  14.       )
  15.     )
  16.   )
  17.   (princ)
  18. )

The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #29 on: November 21, 2023, 02:31:18 AM »
anyway, when there is a solid hatch, there are allways problems...
... but it doesn't depend from your great xdrx-entity-box function ...
which maybe should be called xdrx-ss-box

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #30 on: November 21, 2023, 03:51:41 AM »
but it also fails when there are blocks that are not uniformly scaled
(even if they do not contain solid hatches)...

xdcad

  • Swamp Rat
  • Posts: 527
Re: bounding box along direction
« Reply #31 on: November 21, 2023, 05:26:57 AM »
but it also fails when there are blocks that are not uniformly scaled
(even if they do not contain solid hatches)...

Pls Upload a DWG that encounters problems。
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

xdcad

  • Swamp Rat
  • Posts: 527
Re: bounding box along direction
« Reply #32 on: November 21, 2023, 05:33:01 AM »
anyway, when there is a solid hatch, there are allways problems...
... but it doesn't depend from your great xdrx-entity-box function ...
which maybe should be called xdrx-ss-box

In order not to classify so many names, they are all classified as entity.
Entity can handle a single entity, but also supports tables or selection sets of multiple entities.

If it is called ss, will the same problem still occur when only one is processed? :smitten:
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #33 on: November 21, 2023, 06:13:18 AM »
Quote
Pls Upload a DWG that encounters problems。

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #34 on: November 21, 2023, 06:25:56 AM »
Quote
If it is called ss, will the same problem still occur when only one is processed ?
No.
It will be a selection set that contains only 1 entity.

a rule could be that
when a function processes only 1 entity name,
its name has to contain the word "entity",
while if it processes a selection set,
its name must contain the word "ss" or "sset" . . .

. . .
however this is not a big problem
. . .







xdcad

  • Swamp Rat
  • Posts: 527
Re: bounding box along direction
« Reply #35 on: November 21, 2023, 09:51:46 AM »
but it also fails when there are blocks that are not uniformly scaled
(even if they do not contain solid hatches)...

Solved, it is a problem that has always existed in AUTOCAD itself, that is, there is a deviation in the SPLINE bounding box.
Ordinary SPLINE, API has been solved,
However, in the block, the method of finding the bounding box is to use ARX's AcDbSpatialFilter::queryBounds(...) method, but there is still a problem.

Now I use my own method to process SPLINE in Block separately.

The API has been updated, please try downloading it again.

The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

domenicomaria

  • Swamp Rat
  • Posts: 743
Re: bounding box along direction
« Reply #36 on: November 21, 2023, 11:55:25 AM »
yes, with splines contained in a not uniformly scaled insert, works ...

but still not, with a solid hatch !

xdcad

  • Swamp Rat
  • Posts: 527
Re: bounding box along direction
« Reply #37 on: November 21, 2023, 12:44:19 PM »
yes, with splines contained in a not uniformly scaled insert, works ...

but still not, with a solid hatch !

That's because your HATCH has a border, and the grip points of the border SPLINE are all within the range, and the data is stored in the HATCH object.
So that's all it takes, the result is correct.
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net