-
Notifications
You must be signed in to change notification settings - Fork 3
/
mhatch.lsp
66 lines (64 loc) · 3.19 KB
/
mhatch.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
```
This function, `c:mhatch`, creates a hatch pattern for selected entities in AutoCAD. It first checks if the selected entities are either a LWPOLYLINE, POLYLINE, CIRCLE, or ELLIPSE. If the entity is a closed polyline, a circle, or a closed curve, it creates a hatch using the current system variables for hatch pattern name, scale, and angle. The hatch is created in either the model space or paper space depending on the current viewport. The function then prints a newline character to the command line. This function is a great example of how to use AutoLISP to automate tasks in AutoCAD.
```
(defun c:mhatch (/ ang do-it doc hatch oname pname scl space ss)
; Check if the user has selected any entities of type LWPOLYLINE, POLYLINE, CIRCLE, or ELLIPSE
(if (setq ss (ssget '((0 . "LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE"))))
(progn
; Get the current system variables for hatch pattern scale, angle, and name
(setq scl (getvar "hpscale") ; Hatch pattern scale
ang (getvar "hpang") ; Hatch pattern angle
pname (getvar "hpname") ; Hatch pattern name
; Check if hatch patterns are associative
hpassoc (if (= (getvar "hpassoc") 1)
:vlax-true
:vlax-false)
; Get the active document
doc (vla-get-activedocument
(vlax-get-acad-object))
; Check if the current viewport is paper space or model space
space (if (= (getvar "cvport") 1)
(vla-get-paperspace doc)
(vla-get-modelspace doc)
)
)
; Loop through each entity in the active selection set
(vlax-for ent (vla-get-activeselectionset doc)
(setq do-it nil
; Get the name of the entity
oname (strcase (vla-get-objectname ent)))
; Check the type and properties of the entity
(cond ((vl-string-search "CIRCLE" oname) ; If the entity is a circle
(setq do-it t)
)
((and (vl-string-search "LINE" oname) ; If the entity is a closed polyline
(eq (vla-get-closed ent) :vlax-true)
)
(setq do-it t)
)
((equal (vlax-curve-getstartpoint ent) ; If the entity is a closed curve
(vlax-curve-getendpoint ent)
1e-6)
(setq do-it t)
)
)
; If the entity meets the conditions, create a hatch
(if do-it
(progn
; Create a new hatch in the current space
(setq hatch (vlax-invoke space 'addhatch acHatchObject pname hpassoc))
; Add the entity to the hatch
(vlax-invoke hatch 'appendouterloop (list ent))
; Set the hatch pattern angle and scale
(vlax-put hatch 'patternangle ang)
(vlax-put hatch 'patternscale scl)
; Evaluate the hatch to display it
(vla-evaluate hatch)
)
)
)
)
)
; Print a newline character to the command line
(princ)
)