converting rectangles and horizontal/vertical Polylines into block

converting rectangles and horizontal/vertical Polylines into block

Anonymous
Not applicable
1,237 Views
6 Replies
Message 1 of 7

converting rectangles and horizontal/vertical Polylines into block

Anonymous
Not applicable

Hello experts, How can I convert rectanges and horizontal Polylines to block1 and rectanges and vertical Polylines to block2.

 

Looking forward to your reply, thanks.

0 Likes
1,238 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

 

You should get some inspiration from this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

Anonymous
Not applicable

Dear Gilles,

 

Thanks for your reply, I found some useful codes, and could convert rectanges into block, but still could not convert rectangles + horizontal Polylines into block1 together, and rectangles + vertical Polylines into block2 together.

 

I would be most grateful if you could give me some help.

 

(defun c:ctb(/ ss adoc pt_lst center blk *error* lst bpat bname bi first)
  (defun *error* (msg)
    (vla-endundomark adoc)
    (princ msg)
    (princ)
    )
  (setq bpat "MYBLOCK")
  (vl-load-com)
  (vla-startundomark
    (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
    )
  (if (not (vl-catch-all-error-p
             (vl-catch-all-apply '(lambda () (setq ss (ssget "_X" '((0 . "LWPOLYLINE") (8 . "MyLayer") (70 . 1) (90 . 4))))))
             )
           )
    (progn
      (mapcar '(lambda(item)
      (setq
 ss (list item)
        pt_lst (apply 'append
                      (mapcar
                        '(lambda (x / minp maxp)
                           (vla-getboundingbox x 'minp 'maxp)
                           (list (vlax-safearray->list minp)
                                 (vlax-safearray->list maxp)
                                 )
                           )
                        ss
                        )
                      )
        center (mapcar '(lambda (a b) (/ (+ a b) 2.))
                       (list (apply 'min (mapcar 'car pt_lst))
                             (apply 'min (mapcar 'cadr pt_lst))
                             (apply 'min (mapcar 'caddr pt_lst))
                             )
                       (list (apply 'max (mapcar 'car pt_lst))
                             (apply 'max (mapcar 'cadr pt_lst))
                             (apply 'max (mapcar 'caddr pt_lst))
                             )
                       )
 )
 (if (null first)
   (progn
     (setq
       bname  
      (progn
        (setq bi 0)
        (while (tblsearch "BLOCK" (setq bname (strcat bpat (itoa(setq bi(1+ bi)))))))
        bname)
       blk
      (vla-add (vla-get-blocks adoc)
                        (vlax-3d-point center)
                        bname
        )
       )
    (vla-copyobjects
        adoc
        (vlax-make-variant
          (vlax-safearray-fill
            (vlax-make-safearray vlax-vbobject (cons 0 (1- (length ss))))
            ss
            )
          )
        blk
        )
     (setq first t)
   )
 )
     
      (vla-insertblock
        (vla-objectidtoobject adoc (vla-get-ownerid (car ss)))
        (vlax-3d-point center)
        (vla-get-name blk)
        1.0
        1.0
        1.0
        0.0
        )
   )
   (setq
        lst     (mapcar 'vlax-ename->vla-object
                       (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
                       )
 )
       )
      
      (mapcar 'vla-erase lst)
      )
    )
  (vla-endundomark adoc)
  (princ)
  )
0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

Clarify some things:

 

Do you want to select the rectangle and linear Polyline to be defined as Block1 or Block2, or do you want a routine to find them?  I assume the latter, but....

 

Do you already have Block1 and Block2 defined, and you want to find all rectangles with touching lienar Polylines, and replace all those situations with an insertion of Block1 or Block2 depending on the direction of the linear Polyline?  Or do you want to include making a rectangle-with-horizontal-Polyline into the definition of Block1 as part of the routine, and making a rectangle-with-vertical-Polyline into the definition of Block2?

 

And if a routines finds the pieces and defines the Blocks, then will there be more of them, so that you then want all remaining combinations replaced with insertions of appropriate Blocks?

 

Might some Block1 situations have their horizontal Polyline on the right side, or Block2 situations have their vertical Polyline on the top, and if so, should they be replaced with an insertion of the appropriate Block rotated 180 degrees?

 

Where should the insertion point be defined?  At the middle of the rectangle, as it appears your posted code would calculate?  At the far end of the linear Polyline?  At the point where they meet?  Somewhere else?

 

Would they all be the same size rectangles and the same length Polylines for each Block type [the two rectangles are not the same size in your drawing, and the linear two Polylines are not the same length]?  If they are not exactly the same size, would you want to replace them all with the same Block anyway, and force them to all be the same size?  Or perhaps use different insertion scale factors?

 

Those linear Polylines are peculiar in two ways that I notice:

 

1.  They're closed, which doesn't seem logical, but you may have a reason for it.  But it makes the task more difficult if you want the routine to find the pieces and define the Blocks.  To identify a rectangle and a linear Polyline that touches at one end it would usually involve checking the start and end points of the Polyline for whether they meet the rectangle, but if the start and end points are at the same place, it might not be the end that touches the rectangle, so that check would miss the connection.  If they might be closed, it would require a different kind of check, though it's certainly possible.

 

2.  The Properties box for the linear Polylines shows the same values [15] for the start and end widths at both vertices, but doesn't show a Global width as one would expect.  They must be off by some tiny amount somewhere.

 

Also, the code you posted will miss any Polylines with linetype generation enabled, because of the (70 . 1) entry in the (ssget) filter list that is looking for closed ones.  If one is closed with linetype generation, that will be (70 . 129), and your code will not find it.  But putting the sequential terms  (-4 . "&") (70 . 1) in the filter list will.

Kent Cooper, AIA
0 Likes
Message 5 of 7

Anonymous
Not applicable

Thank you so much for your time and consideration.

 

I want to convert all rectangles with horizontal Polylines into block1, all rectangles with vertical Polylines into block2 using a lisp routine. Attached is my defined two blocks manually.

 

Maybe it's too complicated. or is it possible to define the horizontal Polylines as block 1 and vertical Polylines as block 2 respectively using a lisp routine ?

0 Likes
Message 6 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... Attached is my defined two blocks manually.

 


It may be possible to identify the combinations of rectangles and linear Polylines with a routine, but you must re-define those Blocks to have their insertion points in some logical relationship to their contents [several possibilities inquired about in my previous reply], or a routine will have to be more complicated than necessary to Insert them appropriately in relation to the locations of qualifying pieces that it finds.

 

There are several other questions in my previous reply that have not yet been answered.  And I could come up with more, such as:  might these ever be at non-orthogonal angles?

Kent Cooper, AIA
0 Likes
Message 7 of 7

Anonymous
Not applicable

Please find below answers to your questions.

 

Do you want to select the rectangle and linear Polyline to be defined as Block1 or Block2, or do you want a routine to find them?  I assume the latter, but....
My answer is: I don't want to select them one by one, I want to a routine to find them and can be defined as Block1 or Block2 automatically.

 

Do you already have Block1 and Block2 defined, and you want to find all rectangles with touching lienar Polylines,
My answer is: No, I don't yet have Block1 and Block2 defined.


and replace all those situations with an insertion of Block1 or Block2 depending on the direction of the linear Polyline?
My answer is: Yes


Or do you want to include making a rectangle-with-horizontal-Polyline into the definition of Block1 as part of the routine, and making a rectangle-with-vertical-Polyline into the definition of Block2?
My answer is: Yes


And if a routines finds the pieces and defines the Blocks, then will there be more of them, so that you then want all remaining combinations replaced with insertions of appropriate Blocks?

My answer is: Yes

 

Might some Block1 situations have their horizontal Polyline on the right side, or Block2 situations have their vertical Polyline on the top, and if so, should they be replaced with an insertion of the appropriate Block rotated 180 degrees?
My answer is: For Block1, it is not right side, only their horizontal Polyline on the left side.
For Block2, it is not top side, only their vertical Polyline on the bottom side.

 

Where should the insertion point be defined?  At the middle of the rectangle, as it appears your posted code would calculate?  At the far end of the linear Polyline?  At the point where they meet?  Somewhere else?
My answer is: Not be defined, but we don't need to consider their position.

 

Would they all be the same size rectangles and the same length Polylines for each Block type [the two rectangles are not the same size in your drawing, and the linear two Polylines are not the same length]?  If they are not exactly the same size, would you want to replace them all with the same Block anyway, and force them to all be the same size?  Or perhaps use different insertion scale factors?
My answer is: Yes, their lengths are different, it is better to force them to all be the same size, but this is not important.

 

 

I appreciate your help.

 

0 Likes