Merging 2 blocks to 1 block and assigning each a visibility state automatically.

Merging 2 blocks to 1 block and assigning each a visibility state automatically.

reemasaf44
Explorer Explorer
512 Views
8 Replies
Message 1 of 9

Merging 2 blocks to 1 block and assigning each a visibility state automatically.

reemasaf44
Explorer
Explorer

Is it possible to create a lisp that does the described in the title? for I have to blocks that are the same the colors are just different and I want to layer them on top of each other and be able to switch them until now I did this manually and created a block and the visibility state for each 1. But, I have hundreds  of blocks that need that exact same thing. I can probably create a macro that will get me close but even that will be a really complicated macro...

0 Likes
513 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

What if you just give whatever parts are in different colors between them a color assignment of BYBLOCK?  Then [if I understand correctly what's going on] you should not need Visibility States -- you can just assign a color to a Block Insertion/Reference, and those parts in that Insertion will show in that color.

Kent Cooper, AIA
0 Likes
Message 3 of 9

reemasaf44
Explorer
Explorer

You are halfway right, because even if I put what ever colors I want to change the I can only use a single color and the change between the two blocks are sometimes many different colors resulting in a color pallet for each different blocks thus making you solution irrelevant...check the block I uploaded for reference I will put it here aswell.

0 Likes
Message 4 of 9

reemasaf44
Explorer
Explorer

just to clarify one small thing the block I uploaded is as simple as it can get and even it needs to be different colors for each part, most of my blocks also contain text that needs to be changed colors which is why just copying the same block and changing the colors and assigning each block a visibility state is the only viable solution I'm seeing in the first place... I work in traffic arrangements so I always get new signs that need to be both colorful and gray by the color pallets I presented...

0 Likes
Message 5 of 9

Sea-Haven
Mentor
Mentor

If there traffic signs are the colors not fixed ? So draw each item in the block with correct color. I can not see much effort in have 2 blocks true colors and grey "NO U" & "NO U-G"

 

A lisp to copy the block, rename and assign all objects to grey may be more useful.

0 Likes
Message 6 of 9

reemasaf44
Explorer
Explorer

It is required by law that traffic signs that are temporary or are newly places to be colored and that traffic signs that already exist for a long time to be gray... Until now we have been just using 2 separate blocks for each color, but it is a lot more efficient to be able to change one block to either colorful or gray...

0 Likes
Message 7 of 9

Sea-Haven
Mentor
Mentor

Need to be a dynamic block, google for youtube make visibilty states, should be able to select a block and make the 2 visibilty states via a lisp. Hence do lots in one go. Second state would be a copy of color state but with all objects set to grey.

 

I can not help don't have access to dynamic blocks at moment.

0 Likes
Message 8 of 9

reemasaf44
Explorer
Explorer

My god that took an incredible amount of hours learning how to write lisps and all the basic syntax but at the end I reached my desired result to a tea! there was hours upon hours of debugging but I managed to pull it of!!!

here is the end result for anyone who is interested:

 

(defun c:MergeBlocks ()
  (setq block1 (car (entsel "\nSelect the first block: ")))
  (setq block2 (car (entsel "\nSelect the second block: ")))
 
  (setq basePoint1 (getpoint "\nPick a base point for the first block: "))
  (setq basePoint2 (getpoint "\nPick a base point for the second block: "))
  (setq blockBasePoint (getpoint "\nPick a base point for the merged block: "))

  ; Move the second block to align with the first block
  (command "_.MOVE" block2 "" basePoint2 basePoint1)

  ; Get the names of the first and second blocks
  (setq block1Name (cdr (assoc 2 (entget block1))))
  (setq block2Name (cdr (assoc 2 (entget block2))))

  ; Create a unique block name by adding 'dynamic'
  (setq mergedBlockName (strcat block1Name "_dynamic"))

  ; Merge the blocks into a new block with the unique name
  (command "-BLOCK" mergedBlockName blockBasePoint block1 block2 "")

  ; Calculate the new basePoint1 relative to the blockBasePoint
  (setq gripBasePoint (list (- (car basePoint1) (car blockBasePoint)) (- (cadr basePoint1) (cadr blockBasePoint)) (caddr basePoint1)))

  ; Go into block editor for the merged block
  (command "_BEDIT" mergedBlockName)
 
    ; Define a filter for selecting entities by name
  (setq block1Filter (list (cons 2 block1Name)))
  (setq block2Filter (list (cons 2 block2Name)))

  ; Select block1 entities by name within the block editor
  (setq block1Entity (ssget "_X" block1Filter))

  ; Select block2 entities by name within the block editor
  (setq block2Entity (ssget "_X" block2Filter))
 
  ; Set the visibility state grip to newBasePoint1
  (command "_bparameter" "_visibility" gripBasePoint "1")
 
  ; Create new visibility state "Colored"
  (command "_bvstate" "_new" "Colored" "_hide")
  (command "_bvstate" "_set" "Colored")
 
  ; Set block1 to visibility state "Colored"
  (command "_bvshow" block1Entity "" "current")
 
  ; Create new visibility state "Colored"
  (command "_bvstate" "_new" "Gray" "_hide")
  (command "_bvstate" "_set" "Gray")
 
  ; Set block1 to visibility state "Colored"
  (command "_bvshow" block2Entity "" "current")
 
  ; Delete default state
  (command "bvstate" "delete" "VisibilityState0")
 
  ; Save and exit
  (command "_bsave")
  (command "_bclose")
 
  ; Insert block at the spot of creation
  (command "_.INSERT" mergedBlockName blockBasePoint "" "" "")
  (princ (strcat "\nBlocks merged into '" mergedBlockName "'."))
)
0 Likes
Message 9 of 9

Sea-Haven
Mentor
Mentor

Glad you got it working, would this possibly be better with points set to 0,0 makes the block handling a little bit easier. Rather than some odd insertion point, I would expect the signs have insertion point say in center of object. 

(setq basePoint1 (getpoint "\nPick a base point for the first block: "))
  (setq basePoint2 (getpoint "\nPick a base point for the second block: "))
  (setq blockBasePoint (getpoint "\nPick a base point for the merged block: "))

 Would using the insertion point of the block make life easier.

 

Just thinking are the 2 blocks similar named  ? Could dump all block names make a list ((blk1 blk2)(blk1 blk2)... then process a lot in one go. 

0 Likes