Author Topic: Replace objects with Blocks?  (Read 3287 times)

0 Members and 1 Guest are viewing this topic.

whdjr

  • Guest
Replace objects with Blocks?
« on: December 05, 2007, 08:20:59 AM »
Do you guys think it would be a possibility to write a tool to replace an object with a block or do you think there are too many unknowns?  I need to do this in a couple hundred places.

My dilemma is I have a dwg with a bunch of rectangles as door threshholds.  I need to add a note to these rect's but I already have a threshhold block with a note in it.  I wanted to get you guy's feedback as to whether you thought it possible to code this or just too many variables.

daron

  • Guest
Re: Replace objects with Blocks?
« Reply #1 on: December 05, 2007, 08:25:22 AM »
I believe it would be a matter of selecting all rectangles, choosing an insertion point from the given rectangles point list (or if you want to use the center of each rectangle as your insertion point, use polar to find it), then insert each block instance into that space, using one of the various looping calls and finally deleting (if that's what you want) the selection set of rectangles. That sounds like between 4-8 variables. Not too many.

Guest

  • Guest
Re: Replace objects with Blocks?
« Reply #2 on: December 05, 2007, 08:25:46 AM »
Right off the top of my head I'd say there are too many unknowns.   Maybe a dynamic block with an alignment parameter would be faster/easier?

Guest

  • Guest
Re: Replace objects with Blocks?
« Reply #3 on: December 05, 2007, 08:26:46 AM »
Daron's idea sounds pretty good... Are all of the threshholds the same size?

whdjr

  • Guest
Re: Replace objects with Blocks?
« Reply #4 on: December 05, 2007, 08:32:40 AM »
Yes the thresholds are all the same size however some have a rotation of 0 and some 90.  The block I intend to insert is Dynamic but it doesn't have an alignment parameter.  I'll put something together and see what I come up with.

Thanks

daron

  • Guest
Re: Replace objects with Blocks?
« Reply #5 on: December 05, 2007, 09:34:56 AM »
So, you're replacing one rectangle for another of the same size? All you have to do to get the angle of point1 and point4. If your block is set up where the rectangle is horizontal, set the insertion point in the block the middle of the rectangle. If there happens to be a block that is rotated 90 from what it should be, have a function created that will rotate it 90 degrees. However, dynablocks are good too.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Replace objects with Blocks?
« Reply #6 on: December 05, 2007, 10:42:44 AM »
I would think it would be a relatively easy task.
The rectangles to be replaced are on  specific layer or maybe two. The rectangles are a specific size
or at least a specific length with a limited range of widths. So selecting them would be a two step
process. Scaleing the block & rotating it shouldn't be a problem.
I've reached the age where the happy hour is a nap. (°ї°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
Re: Replace objects with Blocks?
« Reply #7 on: December 05, 2007, 11:20:54 AM »
The biggest problem would be to insert the block at the correct rotation as it would be different if the original rectangle were drawn clockwise or counterclockwise.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Replace objects with Blocks?
« Reply #8 on: December 05, 2007, 01:38:48 PM »
Something like this could be used.
Note that this is just the start & not a complete solution.

Code: [Select]
(defun c:test()
  (defun rtd (r) (* 180.0 (/ r pi)))
  (setq rec (car(entsel "\nPick Rectangle.")))
  (setq elst (entget rec))
  (setq vlst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) elst)))
  (setq s1 (distance (car vlst) (cadr vlst))
        s2 (distance (car vlst) (last vlst)))
  (if (> s1 s2)
    (setq ang (angle (car vlst) (cadr vlst)))
    (setq ang (angle (car vlst) (last vlst)))
  )
  (if (and (> ang (* 0.5 pi)) (< ang (* 1.5 pi)))  ;flop text at >90 and <270
   (setq ang (+ ang pi))
  )
  ang
)
I've reached the age where the happy hour is a nap. (°ї°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
Re: Replace objects with Blocks?
« Reply #9 on: December 05, 2007, 02:16:49 PM »
Kinda *crude but wouldn't something like this:
Something like this could be used.
Note that this is just the start & not a complete solution.

Code: [Select]
(defun c:test()
  (defun rtd (r) (* 180.0 (/ r pi)))
     (setq cnt 0)
     (setq ss (ssget "x" (list '(8 . "0") '(0 . "LWPOLYLINE"))))
     (repeat (sslength ss)
  (setq elst (entget (ssname ss cnt)))
  (setq vlst (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= 10 (car x))) elst)))
  (setq s1 (distance (car vlst) (cadr vlst))
        s2 (distance (car vlst) (last vlst)))
  (if (> s1 s2)
    (setq ang (angle (car vlst) (cadr vlst)))
    (setq ang (angle (car vlst) (last vlst)))
  )
  (if (and (> ang (* 0.5 pi)) (< ang (* 1.5 pi)))  ;flop text at >90 and <270
   (setq ang (+ ang pi))
  )
(setq cnt (1+ cnt))
)
  ang
)
aleviate the pain of having to hand select each rectangle?

*crude because I just took CAB's code and made it repeat from a selection set and didn't test it or anything.

whdjr

  • Guest
Re: Replace objects with Blocks?
« Reply #10 on: December 05, 2007, 04:09:53 PM »
You guys are awesome and on the ball.  I had jump on something else for today.  I won't be able to look at this more till sometime tomorrow.  See ya later.

VVA

  • Newt
  • Posts: 166
Re: Replace objects with Blocks?
« Reply #11 on: December 06, 2007, 12:21:42 PM »
try this routine
Original posted http://www.caduser.ru/cgi-bin/f1/board.cgi?t=21048Af

Program changes the collection of primitives on the selected primitive. It is first necessary to select the substituting object, then replaceable objects and press Enter. Insert is produced to the center of the limiting (BoundingBox) rectangle of the old objects. The inheritance of the properties of the old primitives

Code: [Select]
(defun c:frto(/ ACTDOC COPOBJ ERRCOUNT EXTLST
       EXTSET FROMCEN LAYCOL MAXPT CURLAY
       MINPT OBJLAY OKCOUNT OLAYST
       SCLAY TOCEN TOOBJ VLAOBJ *ERROR* ASK
_kpblc-ent-properties-copy _kpblc-error-catch)
 
(defun _kpblc-ent-properties-copy (source dest)
 (foreach prop   '("Angle" "Layer" "Linetype" "LinetypeScale" "Lineweight"
        "Normal" "PlotStyleName" "Thickness" "Color" "Visible"
        "Closed" ;|"ConstantWidth" ; не копируется|; "Elevation" "LinetypeGeneration"
        "LinetypeScale" ;|"StartAngle" "EndAngle" ; не копируются|; "Alignment"
        "Backward" "Height" "ObliqueAngle" "Rotation" "ScaleFactor" "StyleName"
        "TextGenerationFlag"  "TextHeight"  "UpsideDown"  "AttachmentPoint" "BackgroundFill"
        "DrawingDirection"  "LineSpacingDistance" "LineSpacingFactor" "LineSpacingStyle"  "Width"
        "XScaleFactor" "YScaleFactor" "ZScaleFactor" ;| Viewport|; "ArcSmoothness" "CustomScale"
        "Direction" "DisplayLocked"  "GridOn" "LensLength" "ModelView" "ShadePlot" "SheetView"
        "SnapBasePoint" "SnapOn" "SnapRotationAngle" "StandardScale" "Target"  "TwistAngle"
        "UCSIconAtOrigin"   "UCSIconOn"     "UCSPerViewport" "ViewportOn")
 (if (and (vlax-property-available-p source prop)(vlax-property-available-p dest prop t))
  (_kpblc-error-catch
    '(lambda ()(vlax-put-property dest prop (vlax-get-property source prop))) nil)))) ;_ end of defun
(defun _kpblc-error-catch
       (protected-function on-error-function / catch_error_result)
  (setq catch_error_result (vl-catch-all-apply protected-function))
  (if (and (vl-catch-all-error-p catch_error_result) on-error-function)
    (apply on-error-function
      (list (vl-catch-all-error-message catch_error_result)))
    catch_error_result)) ;_ end of defun 
  (vl-load-com)
  (defun *ERROR*(msg)
    (if olaySt (vla-put-Lock objLay olaySt)); end if
    (vla-EndUndoMark actDoc)(princ)); end of *ERROR*

  (defun GetBoundingCenter(vlaObj / blPt trPt cnPt)
  (vla-GetBoundingBox vlaObj 'minPt 'maxPt)
      (setq blPt(vlax-safearray->list minPt)
      trPt(vlax-safearray->list maxPt)
      cnPt(vlax-3D-point
      (list
            (+(car blPt)(/(-(car trPt)(car blPt))2))
            (+(cadr blPt)(/(-(cadr trPt)(cadr blPt))2))
            (+(caddr blPt)(/(-(caddr trPt)(caddr blPt))2)) ;_<<< Заменили
            )))); end of GetBoundingCenter
  (setq extSet(ssget "_I"))
 (while (not (setq toObj(entsel "\n+++ Select source object -> ")))
   (princ "\nSource objects isn't selected!"))
  (if(not extSet)
    (progn
      (princ "\n+++ Select destination objects and press Enter <- ")
      (setq extSet(ssget "_:L")))); end if
  (if(not extSet)(princ "\nDestination objects isn't selected!")); end if
  (if (and extSet toObj)
    (progn
      (initget "Yes No")
      (setq ask (getkword "\nRemove destination object [Yes/No] <No>:"))
      (setq actDoc (vla-get-ActiveDocument(vlax-get-Acad-object))
      layCol (vla-get-Layers actDoc)
      extLst (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp
                     (mapcar 'cadr(ssnamex extSet))))
      vlaObj (vlax-ename->vla-object(car toObj))
      objLay (vla-Item layCol (vla-get-Layer vlaObj))
      olaySt (vla-get-Lock objLay)
     fromCen (GetBoundingCenter vlaObj)
      errCount 0  okCount 0); end setq
      (vla-StartUndoMark actDoc)
      (foreach obj extLst
        (setq toCen (GetBoundingCenter obj)
              scLay (vla-Item layCol (vla-get-Layer obj)));end setq
  (if(/= :vlax-true(vla-get-Lock scLay))
    (progn
    (setq curLay(vla-get-Layer obj))
    (vla-put-Lock objLay :vlax-false)
    (setq copObj(vla-copy vlaObj))
    (vla-Move copObj fromCen toCen)
    (_kpblc-ent-properties-copy obj copObj)
    (vla-put-Layer copObj curLay)
    (vla-put-Lock objLay olaySt)
    (if (= ask "Yes")(vla-Delete obj))
    (setq okCount(1+ okCount))
    ); end progn
    (setq errCount(1+ errCount))
    ); end if
  ); end foreach
      (princ (strcat "\n" (itoa okCount) " were changed. "
    (if(/= 0 errCount)(strcat (itoa errCount) " were on locked layer! ")  "")))
      (vla-EndUndoMark actDoc)); end progn
    (princ "\nSource object isn't selected! ")
    ); end if
  (princ)); end of c:frto

KewlToyZ

  • Guest
Re: Replace objects with Blocks?
« Reply #12 on: December 06, 2007, 08:42:39 PM »
Kewl! New toy!

I wonder how AEC walls and other so called "Smart Objects" will affect its selection.

I'll have to play with this.

whdjr

  • Guest
Re: Replace objects with Blocks?
« Reply #13 on: January 02, 2008, 08:30:19 AM »
Thanks for the code VVA, but it does match rotation and it doesn't put it in the same location.  It's off by a little bit, maybe a foot or so.

Will