Hatchedit only changes one

Hatchedit only changes one

C.Utzinger
Collaborator Collaborator
2,290 Views
33 Replies
Message 1 of 34

Hatchedit only changes one

C.Utzinger
Collaborator
Collaborator

 HI

 

I have the following code, but the Problem is it works only for one hatch. If I select more than one, then only the last selected one changes.

 

What can I do?

 

 

(defun c:<Test5 ( / *error* layr cmd SPIhatch)

	(defun *error* (errmsg)
    	   (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      	   (princ (strcat "\nError: " errmsg)))
    	   (setvar 'CLAYER layr)
    	   (setvar 'CMDECHO cmd)
	   (command-s "_.undo" "_end")
    	   (princ "\n"))

;------------------------------------------------

     	(setq layr (getvar 'CLAYER)
              cmd (getvar 'CMDECHO))	
	
	(setq SPIhatch (ssget "I"))

	(if (not SPIhatch)
		 (progn
		   (command "_.undo" "_begin")
		   (setvar 'CMDECHO 0)
  		   (command "_.-LAYER" "_m" "-I-Schraffur" "_co" "8" "-I-Schraffur" "")
		   (setvar 'HPNAME "NET")
		   (command "_.-bhatch" "_l" "-I-Schraffur" "_p" "NET" "2" "0" "_a" "_h" "_y" "_a" "_y" "" "_co" "bylayer" "_n" "")
		   (setvar 'CMDECHO 1)
		   (command "_.-bhatch") 
		   (while (> (getvar "cmdactive") 0) (command pause))
		   (setvar 'CLAYER lay)
  		   (setvar 'CMDECHO cmd)
		   (command "_.undo" "_end")
		   (prin1))
		
		 (progn
		   (command "_.undo" "_begin")
  		   (command "_.-LAYER" "_m" "-I-Schraffur" "_co" "8" "-I-Schraffur" "")
		   (command "_.-hatchedit" SPIhatch "_p" "NET" "2" "0")
		   (command "_.-hatchedit" SPIhatch "LA" "-I-Schraffur")
		   (setvar 'CLAYER layr)
  		   (setvar 'CMDECHO cmd)
		   (prompt "\nSchraffur angepasst und auf Layer -I-Schraffur gewechselt!")
		   (command "_.undo" "_end")
  		   (princ)) 
	)
) ;_ end of defun

 

0 Likes
Accepted solutions (2)
2,291 Views
33 Replies
Replies (33)
Message 21 of 34

C.Utzinger
Collaborator
Collaborator

I have it! Perhaps there is an easier way, but this one works!:

 

 

  (setq SPIhatch (ssget "I" '((0 . "HATCH"))))

 

  (setq SPIhatch2 (ssget "I"))

 

  (if (not (= (SSLENGTH SPIhatch2) (SSLENGTH SPIhatch)))
      (progn
        (alert "Nur Schraffuren wählen! \nNochmals versuchen :P")
        (exit)))

0 Likes
Message 22 of 34

C.Utzinger
Collaborator
Collaborator

Now I have got the final version!!!

 

 

(defun c:<Test3 ( / *error* lay cmd logname SPIhatch SPIhatch2 cnt)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
        (princ (strcat "\nError: " errmsg)))
    	(setvar 'CLAYER lay)
    	(setvar 'CMDECHO cmd)
	(command-s "_.undo" "_end")
	(sssetfirst nil SPIhatch)
    	(princ "\n"))

  (setq lay (getvar 'CLAYER)
        cmd (getvar 'CMDECHO))		

  (setq logname (getvar "loginname"))

  (setq SPIhatch (ssget "I" '((0 . "HATCH"))))
  (setq SPIhatch2 (ssget "I"))

  (if (and SPIhatch2 (not SPIhatch))
          (progn 
	  (alert (strcat "Keine Schraffur gewählt! \n\nSali " logname "! Darfst es nochmals versuchen :D..."))
          (exit)))
  (if (and SPIhatch SPIhatch2 (not (= (SSLENGTH SPIhatch2) (SSLENGTH SPIhatch))))
          (progn 
	  (alert (strcat "Nur Schraffuren wählen! \n\nSali " logname "! Darfst es nochmals versuchen :D..."))
          (exit)))

  (if (not SPIhatch)
      (progn
	(command "_.undo" "_begin")
        (command "_.undo" "_begin")
        (setvar 'CMDECHO 0)
  	(command "_.-LAYER" "_m" "-I-Schraffur-Beton-Schnitt-grau" "_co" "253" "-I-Schraffur-Beton-Schnitt-grau" "")
	(setvar 'HPNAME "SOLID")
	(command "_.-bhatch" "_l" "-I-Schraffur-Beton-Schnitt-grau" "_a" "_h" "_y" "_a" "_y" "" "_co" "bylayer" "")  
	(setvar 'CMDECHO 1)
	(command "_.-bhatch") 
	(while (> (getvar "cmdactive") 0) (command pause))
	(setvar 'CLAYER lay)
  	(setvar 'CMDECHO cmd)
	(command "_.undo" "_end")
	(prin1))
      (progn
	(command "_.undo" "_begin")
  	(command "_.-LAYER" "_m" "-I-Schraffur-Beton-Schnitt-grau" "_co" "253" "-I-Schraffur-Beton-Schnitt-grau" "")
        (SETQ cnt 0)
        (WHILE (< cnt (SSLENGTH SPIhatch))
          (command "_.-hatchedit" (SSNAME SPIhatch cnt) "_p" "SOLID")
          (command "_.-hatchedit" (SSNAME SPIhatch cnt) "_l" "-I-Schraffur-Beton-Schnitt-grau")
          (SETQ cnt (1+ cnt)))
        (setvar 'CLAYER lay)
        (setvar 'CMDECHO cmd)
        (prompt "\nSchraffur angepasst und auf Layer -I-Schraffur-Beton-Schnitt-grau gewechselt!")
        (command "_.undo" "_end")
        (princ)) 	
   )
) ;_ end of defun

Thank you!!!!

0 Likes
Message 23 of 34

ВeekeeCZ
Consultant
Consultant

Time to learn the (cond) finction !!

And some more... untested.

 

(vl-load-com)

(defun c:<Test3 ( / *error* adoc lay cmd logname SPIhatch SPIno-hatch i)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))

    (setvar 'CLAYER lay)
    (setvar 'CMDECHO cmd)
    (sssetfirst nil SPIhatch)
    (vla-endundomark adoc)

    (princ "\n")
    (princ))

  ; -----------------------------------------------------------------------------------------------------
  
  (setq lay (getvar 'CLAYER)
        cmd (getvar 'CMDECHO)
	logname (getvar 'LOGINNAME))

  (vla-endundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (vla-startundomark adoc)
  
  (setq SPIhatch (ssget "I" '((0 . "HATCH")))
	SPIno-hatch (ssget "I"))

  (cond ((and SPIno-hatch
	      (not SPIhatch))
	 (alert (strcat "Keine Schraffur gewählt! \n\nSali " logname "! Darfst es nochmals versuchen :D...")))

	((and SPIhatch
	      SPIno-hatch
	      (/= (sslength SPIno-hatch)
		  (sslength SPIhatch)))
	 (alert (strcat "Nur Schraffuren wählen! \n\nSali " logname "! Darfst es nochmals versuchen :D...")))

	((not SPIhatch)
	 (setvar 'CMDECHO 0)
	 (command "_.-LAYER" "_m" "-I-Schraffur-Beton-Schnitt-grau" "_co" "253" "-I-Schraffur-Beton-Schnitt-grau" "")
	 (setvar 'HPNAME "SOLID")
	 (command "_.-bhatch" "_l" "-I-Schraffur-Beton-Schnitt-grau" "_a" "_h" "_y" "_a" "_y" "" "_co" "bylayer" "")
	 (setvar 'CMDECHO 1)
	 (command "_.-bhatch")
	 (while (> (getvar 'CMDACTIVE) 0) (command pause)))

	(T
	 (command "_.-LAYER" "_m" "-I-Schraffur-Beton-Schnitt-grau" "_co" "253" "-I-Schraffur-Beton-Schnitt-grau" "")
	 (repeat (setq i (sslength SPIhatch))
	   (command "_.-hatchedit" (ssname SPIhatch (setq i (1- i))) "_p" "SOLID"
		    "_.-hatchedit" (ssname SPIhatch i) "_l" "-I-Schraffur-Beton-Schnitt-grau"))
	 (prompt "\nSchraffur angepasst und auf Layer -I-Schraffur-Beton-Schnitt-grau gewechselt!")))
  (*error* "end")
)

 Gute Nacht! Sorry for digging into "final" - there should be a lock button to prevent from those guys!

0 Likes
Message 24 of 34

C.Utzinger
Collaborator
Collaborator

You my Hero!!!!

 

I tried something with COND, but only crab was coming out...Smiley LOL

 

There is only one problem i think. The code crashes if you only select one hatch, it breaks in the hatchedit command.

 

Thank you in advance... 

 

  

0 Likes
Message 25 of 34

ВeekeeCZ
Consultant
Consultant
Would you post a test dwg?
0 Likes
Message 26 of 34

C.Utzinger
Collaborator
Collaborator

Of course...

0 Likes
Message 27 of 34

ВeekeeCZ
Consultant
Consultant

I don't understand why is that so complicated. 

 

What's the purpose of this line?

(and SPIhatch SPIhatch2 (not (= (SSLENGTH SPIhatch2) (SSLENGTH SPIhatch))))   

my reading is if the was pre-selected some hathes AND some hathes with something else (lwpolylines?) AND the both groups does not have the same mount of members.

 

Why not just adjust HATCHes if there were pre-selected, othewise let the user make new one. Not care about polylines...

0 Likes
Message 28 of 34

C.Utzinger
Collaborator
Collaborator

OK, it's just when you select the hatches one by one and per accident get another object than a hatch.

 

If you have 20 hatches and you got selected 18 and per accident 2 other objects, and the 18 changes and the other 2 not, perhaps you don't notice that...

 

It is just to advice that you have selected an other object.

 

But this is not the Problem i think, because that is working fine. It's just when you select only one hatch for change, then comes the hachtedit menue and the Routine crashes.

 

 

 

Sorry about my english!

 

0 Likes
Message 29 of 34

ВeekeeCZ
Consultant
Consultant

You should filter them out automatically and not bother the users.

 

I really like the Tool Palettes for HATCHinch, see how it works... HERE - it's very easy to set layer, color, hatch pattern, all returns back after the usage. 

 

0 Likes
Message 30 of 34

C.Utzinger
Collaborator
Collaborator

Thank you.

 

I know them, but we are getting away from them and just want to work with Ribbons.

 

 

 

0 Likes
Message 31 of 34

ВeekeeCZ
Consultant
Consultant

Hmm... bad for you.

 

Well, here is my version. Really don't like your alerts.

 

(vl-load-com)

(defun c:<Test3 ( / *error* oVAR nVAR adoc ss)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))

    (mapcar 'setvar nVAR oVAR)
    (vla-endundomark adoc)
    
    (princ))

  ; -----------------------------------------------------------------------------------------------------------
  ; -----------------------------------------------------------------------------------------------------------

  (setq oVAR (mapcar 'getvar (setq nVAR '(CMDECHO CLAYER HPNAME HPLAYER))))
  
  (vla-endundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (vla-startundomark adoc)
  
  (setq ss (ssget "_I" '((0 . "HATCH"))))

  (setvar 'CMDECHO 0)
  (setvar 'HPNAME "SOLID")
  (setvar 'HPLAYER ".")
  (command "_.-LAYER" "_M" "-I-Schraffur-Beton-Schnitt-grau"
           "_CO" "253" "-I-Schraffur-Beton-Schnitt-grau"
           "_T" "-I-Schraffur-Beton-Schnitt-grau"
           "_S" "-I-Schraffur-Beton-Schnitt-grau"
            "")
  
  (if ss
    (progn
      (initcommandversion)
      (command "_.-HATCHEDIT" ss "" "_P" "Solid")
      (initcommandversion)
      (command "_.-HATCHEDIT" ss "" "_Layer" "-I-Schraffur-Beton-Schnitt-grau")
      (prompt "\nSchraffur angepasst und auf Layer -I-Schraffur-Beton-Schnitt-grau gewechselt!"))
    (progn
      (command "_.-BHATCH" "_a" "_h" "_y" "_a" "_y" "" "_co" "bylayer" "")
      (setvar 'CMDECHO 1)
      (command "_.-BHATCH")
      (while (> (getvar 'CMDACTIVE) 0) (command PAUSE))))
  	 
  (*error* "end")
)

 

0 Likes
Message 32 of 34

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok, one thing still bothers me... the routine fails if all selected objects are on locked layer. There is no easy way to do that using the (ssget)'s filters - the combination with Implied selection is the issue. What we can do is prevent the (command) from the failing using (command-s) instead (blue ones)..., but your following prompt will show not matter if the hatchedit was success or failed. 

 

So I made the sub-function which exclude objects on lock layers from the given selection.

 

 

(vl-load-com)

(defun c:<Test3 ( / *error* oVAR nVAR adoc :sslockless ss)

  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))

    (mapcar 'setvar nVAR oVAR)
    (vla-endundomark adoc)
    
    (princ))

  (defun :sslockless (ss / i en)
    (repeat (setq i (sslength ss))
      (if (= 4 (logand 4 (cdr (assoc 70 (tblsearch "LAYER" (cdr (assoc 8 (entget (setq en (ssname ss (setq i (1- i))))))))))))
        (ssdel en ss)))
    (if (and ss (< 0 (sslength ss))) ss nil))
  

  ; -----------------------------------------------------------------------------------------------------------
  ; -----------------------------------------------------------------------------------------------------------

  (setq oVAR (mapcar 'getvar (setq nVAR '(CMDECHO CLAYER HPNAME HPLAYER))))
  
  (vla-endundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (vla-startundomark adoc)
  
  (setq ss (ssget "I" '((0 . "HATCH"))))

  (setvar 'CMDECHO 0)
  (setvar 'HPNAME "SOLID")
  (setvar 'HPLAYER ".")
  (command "_.-LAYER" "_M" "-I-Schraffur-Beton-Schnitt-grau"
           "_CO" "253" "-I-Schraffur-Beton-Schnitt-grau"
           "_T" "-I-Schraffur-Beton-Schnitt-grau"
           "_S" "-I-Schraffur-Beton-Schnitt-grau"
            "")
  (cond ((and ss
              (not (setq ss (:sslockless ss)))
              )
         (sssetfirst nil nil)
         (alert "All selected objects were on locked layers."))
        
        (ss
         (initcommandversion)
         (command "_.-HATCHEDIT" ss "" "_P" "Solid")
         (initcommandversion)
         (command "_.-HATCHEDIT" ss "" "_Layer" "-I-Schraffur-Beton-Schnitt-grau")
         (prompt "\nSchraffur angepasst und auf Layer -I-Schraffur-Beton-Schnitt-grau gewechselt!"))
        
        (T
         (command "_.-BHATCH" "_a" "_h" "_y" "_a" "_y" "" "_co" "bylayer" "")
         (setvar 'CMDECHO 1)
         (command "_.-BHATCH")
         (while (> (getvar 'CMDACTIVE) 0) (command PAUSE))))
  	 
  (*error* "end")
)
0 Likes
Message 33 of 34

C.Utzinger
Collaborator
Collaborator

WOW thank you...

 

I will look at it on monday, i have to leave now.

 

Nice Weekend...

0 Likes
Message 34 of 34

C.Utzinger
Collaborator
Collaborator

Wow!

 

Thank you very much.

 

I also editet my prompt, with an advice that locked object didn't Change.

 

 

Kind regards

0 Likes