-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAlignToDirection.lsp
More file actions
313 lines (301 loc) · 14.7 KB
/
AlignToDirection.lsp
File metadata and controls
313 lines (301 loc) · 14.7 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;
;;; Align To Direction ;;;
;;; Created by Jonathan Handojo ;;;
;;; ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;
;;; This routine allows users to align any objects to a certain direction that contains any ;;;
;;; one of the following properties: ;;;
;;; ;;;
;;; 1. Insertion Point ;;;
;;; 2. Text Position ;;;
;;; 3. Text Alignment Position ;;;
;;; ;;;
;;; Upon issuing the command ATD, the user will be prompted a selection of objects without any ;;;
;;; filters. This will then be filtered after the selection set, highlighting the list of ;;;
;;; objects to be aligned. The user will be prompted the direction by clicking two point, or ;;;
;;; one of the following options: ;;;
;;; ;;;
;;; 1. X-direction, ;;;
;;; 2. Y-direction (relative to the UCS) ;;;
;;; 3. "PerpendicularToCurve" - the user clicks a point on any curve objects and the ;;;
;;; direction will be perpendicular to the curve at that specified point. ;;;
;;; 4. "AlongCurve" – similar to step 3, but as opposed to perpendicular, it will be the ;;;
;;; direction of the curve at that specified point. ;;;
;;; ;;;
;;; Curve Selection is compatible with all curves within complex objects, regardless of depth. ;;;
;;; Several examples include curves found in nested blocks, xrefs, tables, dimensions, etc. ;;;
;;; ;;;
;;; The user can next specify the point where all the objects will be aligned to that ;;;
;;; direction. Simply put, it's like aligning marbles on a table using a ruler. Except unlike ;;;
;;; marbles, AutoCAD entities do not keep rolling and stick to your cursor. ;;;
;;; ;;;
;;; Objects that I have successfully worked with this program include: ;;;
;;; ;;;
;;; 1. Tables ;;;
;;; 2. Dimensions ;;;
;;; 3. Blocks ;;;
;;; 4. OLE Images ;;;
;;; 5. Texts and MTexts (regardless of justification) ;;;
;;; ;;;
;;; This program will work under any UCS if the objects to translate resides on the same XY- ;;;
;;; plane as the UCS. I am not particularly good at coordinate transformations, so this is the ;;;
;;; least I can do. ;;;
;;; ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;
;;; Versions and updates ;;;
;;; ------------------------------------------------------------ ;;;
;;; ;;;
;;; Version 1.0 – First release (27/04/20) ;;;
;;; ;;;
;;; Version 1.1 (23/11/2020) – Included "Perpendicular to Curve" and "Along Curve" option. ;;;
;;; ;;;
;;; Version 1.2 (06/10/2021) – Curve selection enhanced compatibility with nested complex ;;;
;;; objects and blocks. ;;;
;;; ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:atd (/ *error* acadobj activeundo adoc alpts ang ang1
ang2 blk gr grp grv halfpi movang movdis movpt
msp p1 p2 p3 snapang snp snpang ss sse
x y
)
(defun *error* ( msg )
(setvar 'snapang snp)
(if sse (mapcar '(lambda (x) (redraw x 4)) sse))
(vla-EndUndoMark adoc)
(if (not (wcmatch (strcase msg T) "*break*,*cancel*,*exit*"))
(princ (strcat "Error: " msg))
)
)
(setq acadobj (vlax-get-acad-object)
adoc (vla-get-ActiveDocument acadobj)
blk (vla-get-blocks adoc)
msp (vla-get-ModelSpace adoc)
activeundo nil
)
(if (= 0 (logand 8 (getvar "UNDOCTL"))) (vla-StartUndoMark adoc) (setq activeundo T))
(setq snp (getvar 'snapang))
(if
(setq ss
(vl-remove-if-not
'(lambda (x)
(and
(vlax-write-enabled-p x)
(or
(vlax-property-available-p x 'insertionpoint)
(and
(eq (strcase (vla-get-ObjectName x)) "ACDBBLOCKREFERENCE")
(eq (vla-get-IsXRef (vla-item blk (vla-get-EffectiveName x))) :vlax-false)
)
(vlax-property-available-p x 'textposition)
(vlax-property-available-p x 'textalignmentpoint)
(vlax-property-available-p x 'center)
)
)
)
(atd:selset-to-list-vla (ssget "_:L"))
)
)
(progn
(setq sse (mapcar 'vlax-vla-object->ename ss))
(mapcar '(lambda (x) (redraw x 3)) sse)
(setq
ang (progn (initget 1 "X Y PErpendicularToCurve ALongCurve") (getangle "\nSpecify alignment direction [X/Y/PErpendicularToCurve/ALongCurve]: "))
ang (+
(setq halfpi (* 0.5 pi))
(cond
( (numberp ang) ang)
( (eq ang "X") 0)
( (eq ang "Y") halfpi)
( (wcmatch ang "PErpendicularToCurve,ALongCurve")
(+
(atd:GetDirectionAtCurve "\nSpecify point at any curve: ")
(if (eq ang "PErpendicularToCurve") halfpi 0)
)
)
)
)
p1 (trans (vlax-get (car ss) (atd:objprop (car ss))) 0 1)
p2 (polar p1 ang 1)
alpts
(mapcar
'(lambda (x / p3)
(list x
(atd:objprop x)
(inters p1 p2
(setq p3 (trans (vlax-get x (atd:objprop x)) 0 1))
(polar p3 (+ halfpi ang) 1)
nil
)
)
)
ss
)
)
(setvar 'snapang ang)
(while
(progn
(setq gr (grread T 15 0)
grp (last gr)
grv (car gr)
)
(cond
( (= grv 5)
(setq
movpt (inters grp (polar grp ang 1) p1 (polar p1 (- ang halfpi) 1) nil)
movang (angle p1 movpt)
movdis (distance p1 movpt)
)
(mapcar
'(lambda (x)
(vlax-put (car x) (cadr x) (trans (polar (last x) movang movdis) 1 0))
)
alpts
)
(mapcar '(lambda (x) (redraw x 3)) sse)
T
)
( (or (and (= grv 2) (vl-position grp '(13 32))) (= grv 3)) nil)
( T )
)
)
)
)
)
(mapcar '(lambda (x) (redraw x 4)) sse)
(setvar 'snapang snp)
(if activeundo nil (vla-EndUndoMark adoc))
(princ)
)
(defun atd:objprop (obj)
(cond
( (vlax-property-available-p obj 'insertionpoint)
(if
(and
(vlax-property-available-p obj 'textalignmentpoint)
(null (equal (vlax-get obj 'textalignmentpoint) '(0 0 0) 1e-8))
)
'textalignmentpoint
'insertionpoint
)
)
( (vlax-property-available-p obj 'textalignmentpoint) 'textalignmentpoint)
( (vlax-property-available-p obj 'textposition) 'textposition)
( (vlax-property-available-p obj 'center) 'center)
)
)
(defun atd:GetDirectionAtCurve (msg / crv det mtx pt vec)
(while
(progn
(setvar "errno" 0)
(initget "Exit")
(setq det (nentselp (getpoint msg)))
(cond
( (= (getvar "errno") 7) (princ "\nNothing selected."))
( (member det '("Exit" nil)) nil)
( (not (wcmatch (cdr (assoc 0 (entget (setq crv (car det))))) "LINE,LWPOLYLINE,ARC,ELLIPSE,CIRCLE,SPLINE"))
(princ "\nNo curves detected")
)
( t
(if
(setq pt (trans (cadr det) 1 0) mtx (caddr det))
(setq
mtx (atd:4x4->3x3 mtx)
vec (trans
(mxv
(car mtx)
(vlax-curve-getfirstderiv crv
(vlax-curve-getparamatpoint crv
(vlax-curve-getclosestpointto crv
(mxv (gc:Inv3x3 (car mtx)) (mapcar '- pt (cadr mtx)))
)
)
)
)
0 1 t
)
)
(setq vec (trans (vlax-curve-getfirstderiv crv (vlax-curve-getparamatpoint crv (vlax-curve-getclosestpointto crv pt))) 0 1 t))
)
nil
)
)
)
)
(if vec (angle '(0.0 0.0 0.0) vec))
)
(defun atd:selset-to-list-vla (ss / rtn)
(if ss
(repeat (setq i (sslength ss))
(setq i (1- i) rtn (cons (vlax-ename->vla-object (ssname ss i)) rtn))
)
)
)
(defun atd:4x4->3x3 (mx)
(list
(mapcar
'(lambda (a)
(reverse (cdr (reverse a)))
)
(reverse (cdr (reverse mx)))
)
(mapcar 'last mx)
)
)
;; gc:Inv3x3
;; Return the inverse transformation matrix (3X3)
;;
;; Argument
;; mat: a 3x3 matrix
(defun gc:Inv3x3 (mat / a b c d e f g h i det)
(mapcar
'set
'(a b c d e f g h i)
(mapcar 'float (apply 'append mat))
)
(setq det
(+
(* a e i)
(* b f g)
(* c d h)
(- (* c e g))
(- (* b d i))
(- (* a f h))
)
)
(if (and (/= 0 det) (setq det (/ 1 det)))
(mapcar
'(lambda (v)
(mapcar '(lambda (x) (* x det)) v)
)
(list
(list
(- (* e i) (* f h))
(- (* c h) (* b i))
(- (* b f) (* c e))
)
(list
(- (* f g) (* d i))
(- (* a i) (* c g))
(- (* c d) (* a f))
)
(list
(- (* d h) (* e g))
(- (* b g) (* a h))
(- (* a e) (* b d))
)
)
)
)
)
;; Matrix x Vector - Vladimir Nesterovsky
;; Args: m - nxn matrix, v - vector in R^n
(defun mxv ( m v )
(mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
)