Jump to content

Hatch_Off.lsp - Script for HATCHING from existing hatch with no hatch dialog.


3dwannab

Recommended Posts

Hi,

 

Ever annoyed about having to open the hatch dialog to get inherent props from an existing hatch and then AutoCAD seems to think it fine for the hatch to be non associative too!!!

 

Here is a script I wrote and wanted to share. It simply uses the addselected command to hatch your selected objects even though they might be non associative, this will ensure associative hatches for life........ :D

 

It works with every hatch type you can throw at it (Anno and backgrounds too) while retaining ALL the exact same properties.

I need to get it so it'll add the annotation scales but the script will set the flag for it if the source hatch is annotative.

 

 

PLEASE LET ME NOW WHAT YOU THINK OR DON'T THINK ABOUT IT.

 

 

;; FUNCTION SYNTAX

 

Hatch_Off or HOFF

 

;; ABOUT / NOTES

;; ABOUT / NOTES
;; - Hatches using ADDSELECTED command method, no more Hatch UI (EVER) to pick an existing HATCH.
;; - Automatically changes the HPASSOC variable to 1 then sets it back,
;;   upon error or exiting the command.
;; - First pick the HATCH you want then, select your LWPOLYLINES, CIRCLES or ELLIPSES to HATCH.
;;   This routine is made to only select closed LWPOLYLINES. An easy way to identify them.
;;   You can run a script called PSIMPLE to fix you entire drawing in one go. See here >>> http://www.theswamp.org/index.php?topic=19865.msg244786#msg244786
;; - If you want un-associative HATCH for some reason change '(setvar 'hpassoc 1)' to '0'.
;; - Automatically selects the newly created HATCH/es.
;; - Sends draworder of HATCH behind selected objects.

 

;; FULL CODE

;; --------------------------=={ Hatch_Off }==----------------------------
;; -----------------------------------------------------------------------

;; AUTHOR & ADDITIONAL CODE
;; Author:					3dwannab, Copyright © 2018.
;; Error functions:			LeeMac Help pages. www.lee-mac.com.

;; ABOUT / NOTES
;; - Hatches using ADDSELECTED command method, no more Hatch UI (EVER) to pick an existing HATCH.
;; - Automatically changes the HPASSOC variable to 1 then sets it back,
;;   upon error or exiting the command.
;; - First pick the HATCH you want then, select your LWPOLYLINES, CIRCLES or ELLIPSES to HATCH.
;;   This routine is made to only select closed LWPOLYLINES. An easy way to identify them.
;;   You can run a script called PSIMPLE to fix you entire drawing in one go. See here >>> http://www.theswamp.org/index.php?topic=19865.msg244786#msg244786
;; - If you want un-associative HATCH for some reason change '(setvar 'hpassoc 1)' to '0'.
;; - Automatically selects the newly created HATCH/es.
;; - Sends draworder of HATCH behind selected objects.

;; FUNCTION SYNTAX
;; Short-cut				HOFF
;; Long-cut					Hatch_Off

;; VERSION					DATE			INFO
;; Version 1.0				26-07-2018		Initial release.
;; Version 1.01				27-07-2018		var_hatch_bkgcolouradded to set the hatch background colour to none so that the addselected command doesn't use that instead of the original one picked.

;; TO DO LIST
;; Change the polylines to the current boundary attributes of selected hatch boundary.
;; Put all of the annotative scales to the newly created hatch.
;; If a hatch exists on the selected polylines then delete them and hatch with new.
;; Check if it works in different UCS modes. (NOT SURE)

;; -----------------------------------------------------------------------
;; ---------------------=={ Hatch_Off START }==---------------------------

(defun c:---LOAD_HATCH_OFF (/) (LOAD "Hatch_Off") (c:HOFF))

(defun c:HOFF () (c:Hatch_Off))

(defun c:Hatch_Off ( /
*error*
ent_1
ent_1_vla
ent_1_data
sel_me
ss_1
tmp
var_cmde
var_hatch_ass
var_hatch_bkgcolour
var_os
)

(setq *error* LM:error)
(LM:startundo)

(setq var_cmde (getvar "cmdecho"))
(setq var_hatch_anno (getvar "hpannotative"))
(setq var_hatch_ass (getvar "hpassoc"))
(setq var_hatch_bkgcolour (getvar "hpbackgroundcolor"))
(setq var_os (getvar "osmode"))
(setvar 'cmdecho 0)
(setvar 'osmode 0)
(setvar 'hpbackgroundcolor ".")

(while
(not
	(and
		(setq
			ent_1 (car (entsel "\nPlease select a HATCH to copy.\n: ------------------------------ :\n\nThen select any closed LWPOLYLINE's, CIRCLE's or ELLIPSE's.\n"))
			ent_1_data (if ent_1 (entget ent_1))
			)
		(= (cdr (assoc 0 ent_1_data)) "HATCH")
		(sssetfirst nil)
		(setq ent_1_vla (vlax-ename->vla-object ent_1))

		(progn
			(setq ent_last (entlast)
				sel_me (ssadd)
				)
			(while (setq tmp (entnext ent_last)) (setq ent_last tmp))

			(princ "\nNow select your objects to be HOFF'ified !\n")

			(setq ss_1 (ssget '(
				(-4 . "<OR")
				(-4 . "<AND") (0 . "LWPOLYLINE") (70 . 1) (-4 . "AND>")
				(-4 . "<AND") (0 . "CIRCLE,ELLIPSE") (-4 . "AND>")
				(-4 . "OR>")
				)))

			(if	(IsAnno-p (vlax-ename->vla-object ent_1))
				(setvar 'hpannotative 1)
				)
			(setvar 'hpassoc 1)

			(if ss_1
				(progn
					(command "_.addselected" "_non" ent_1 "_S" ss_1 "" "" )
					(command "._draworder" (entlast) "" "_U" ss_1 "")
					(while (setq ent_last (entnext ent_last))
						(ssadd ent_last sel_me)
						)
					(sssetfirst nil sel_me)
					(princ (strcat "\nYou've been HOFF'd with < "(itoa (sslength ss_1)) (if (> (sslength ss_1) 1) " > objects" " object") " hatched.\n"))
					)
				)
			)(princ)
			)
	)
)

(*error* nil)(princ)

)(princ)

;; -----------------------------------------------------------------------
;; ----------------------=={ Functions START }==--------------------------

(vl-load-com)

(defun IsAnno-p (ent / exd ano)
(vl-load-com)
(and (eq (vla-get-HasExtensionDictionary ent) :vlax-true)
	(setq exd (vla-GetExtensionDictionary ent)
		exd (vla-item exd "AcDbContextDataManager")
		ano (vla-item exd "ACDB_ANNOTATIONSCALES")
		)
	(not (zerop (vla-get-Count ano)))
	)
)

(defun LM:error (errmsg)
(and acDoc (vla-EndUndoMark acDoc))
(and errmsg
	(not (wcmatch (strcase errmsg) "*CANCEL*,*EXIT*"))
	(princ (strcat "\n<< Error: " errmsg " >>\n"))
	)
(setvar 'cmdecho var_cmde)
(setvar 'hpassoc var_hatch_ass)
(setvar 'osmode var_os)
(setvar 'hpannotative var_hatch_anno)
(setvar 'hpbackgroundcolor var_hatch_bkgcolour)
(princ (strcat "\nNo HOFF for you today !\n"))
) (princ)

(defun LM:startundo ()
(setq acDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(or (vla-EndUndoMark acDoc) (vla-StartUndoMark acDoc))
) (princ)

;; -----------------------------------------------------------------------
;; -----------------------=={ Functions END }==-- ------------------------

(princ "\nHatch_Off loaded | Version 1.0 | by 3dwannab.\n")
(princ "\nType \"Hatch_Off\" OR \"HOFF\" to run.\n") (princ)

;; -----------------------------------------------------------------------
;; ----------------------=={ Hatch_Off END }==----------------------------
;; EOL

Edited by 3dwannab
Updated to v1.01
Link to comment
Share on other sites

My work is mostly about loops and terminal connection diagrams so I rarely use hatch myself but it seems to work. Only thing I would change (which is just a matter of taste so feel free 2 disagree) and its just cosmetic , you give user both prompts for selecting hatch and the objects who want to be hatched at the same time. So my very first thought was , do I select pline or hatch first? But of course this is just for first use , next time you know... So not bad for an office tea brewer :-)

Edited by rlx
Link to comment
Share on other sites

My work is mostly about loops and terminal connection diagrams so I rarely use hatch myself but it seems to work. Only thing I would change (which is just a matter of taste so feel free 2 disagree) and its just cosmetic , you give user both prompts for selecting hatch and the objects who want to be hatched at the same time. So my very first thought was , do I select pline or hatch first? But of course this is just for first use , next time you know... So not bad for an office tea brewer :-)

 

Thank you. I make more than just T!!

 

I've only just got to sort of understand LISP'ing.

 

I've taken your suggestion on board and added a princ for selection of objects.

 

The 1st entget prompts for the HATCH with the below onscreen display.

I wanted to follow the workflow of both getting HATCH inherit props. ssget can't have on-screen display prompts AFAIK.

 

HOFF OSD.jpg

 

Updated code above to v1.01. Slight bug with the background color getting overridden if that var is set.

Link to comment
Share on other sites

  • 1 month later...

I've tried to get this to work with nested hatches so I could pick those too but I get the error.

<Bad Entity name: XXXXXXX>

 

Here's the code snippet I'm using.

(command "_.addselected" (car (nentsel))) "_S" pause "" "" )


 

Edited by 3dwannab
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...