对指定图元实现向内偏移的取点办法
本帖最后由 as6424 于 2024-2-1 08:42 编辑<div style="color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, "Courier New", monospace; line-height: 19px; white-space: pre;">
<div>(<span style="color: #569cd6;">vl-load-com</span>)
(<span style="color: #569cd6;">defun</span> c:oo (/ oo_ss ss_length temp_i ss_i_ename ename_list p3_nth)
<span style="color: #6a9955;">;or 判断是否需要切换图层,输入则切换,无输入则保持</span>
(<span style="color: #569cd6;">setq</span> oo_py_fs_str (<span style="color: #569cd6;">getstring</span> <span style="color: #ce9178;">"\nn/w"</span>))
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">not</span> oo_py_dict)
(<span style="color: #569cd6;">setq</span> oo_py_dict <span style="color: #b5cea8;">0</span>))
(<span style="color: #569cd6;">setq</span> oo_py_dict_s (<span style="color: #569cd6;">rtos</span> oo_py_dict <span style="color: #b5cea8;">2</span> <span style="color: #b5cea8;">4</span>))
(<span style="color: #569cd6;">setq</span> oo_py_dict_d (<span style="color: #569cd6;">getreal</span> (<span style="color: #569cd6;">strcat</span> <span style="color: #ce9178;">"\n偏移距离:<"</span> oo_py_dict_s <span style="color: #ce9178;">">\n"</span>)))
<span style="color: #6a9955;">;创建选择集oo_ss,设置图层过滤器()</span>
<span style="color: #6a9955;">; (setq oo_ss (ssget (list (cons 8 layer-name))))</span>
(<span style="color: #569cd6;">setq</span> oo_ss_layer_str (<span style="color: #569cd6;">cdr</span> (<span style="color: #569cd6;">assoc</span> <span style="color: #b5cea8;">8</span> (<span style="color: #569cd6;">entget</span> (<span style="color: #569cd6;">car</span> (<span style="color: #569cd6;">entsel</span> <span style="color: #ce9178;">"\n获取图型所在图层"</span>))))))
(<span style="color: #569cd6;">setq</span> oo_ss (<span style="color: #569cd6;">ssget</span> (<span style="color: #569cd6;">list</span> (<span style="color: #569cd6;">cons</span> <span style="color: #b5cea8;">8</span> oo_ss_layer_str))))
<span style="color: #6a9955;">;or 判断是否需要输入偏移距离,输入则切换,无输入则保持</span>
<span style="color: #6a9955;">; (or</span>
<span style="color: #6a9955;">; (oo_py_dict)</span>
<span style="color: #6a9955;">; (setq oo_py_dict(getreal ))</span>
<span style="color: #6a9955;">; )</span>
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">and</span> (/= oo_py_dict_d <span style="color: #ce9178;">""</span>) (/= oo_py_dict_d <span style="color: #ce9178;">"0"</span>) (/= oo_py_dict_d <span style="color: #b5cea8;">0</span>) (/= oo_py_dict_d <span style="color: #569cd6;">nil</span>))
(<span style="color: #569cd6;">setq</span> oo_py_dict oo_py_dict_d)
)
(<span style="color: #569cd6;">setq</span> ss_length (<span style="color: #569cd6;">sslength</span> oo_ss))
(<span style="color: #569cd6;">setq</span> temp_i <span style="color: #b5cea8;">0</span>)
<span style="color: #6a9955;">;偏移后置为当前图层</span>
(<span style="color: #569cd6;">command</span> <span style="color: #ce9178;">"offset"</span> <span style="color: #ce9178;">"l"</span> <span style="color: #ce9178;">"c"</span> <span style="color: #ce9178;">""</span> <span style="color: #ce9178;">""</span>)
<span style="color: #6a9955;">;将选择集遍历赋值给ss_i_ename (ename) 取出单个图元</span>
(<span style="color: #569cd6;">repeat</span> ss_length
(<span style="color: #569cd6;">setq</span> ss_i_ename (<span style="color: #569cd6;">ssname</span> oo_ss temp_i))
<span style="color: #6a9955;">;对每个ename需要操作的内容,取出list,用于提取数据</span>
(<span style="color: #569cd6;">setq</span> ename_list (<span style="color: #569cd6;">entget</span> ss_i_ename))
(<span style="color: #569cd6;">setq</span> ename_type (<span style="color: #569cd6;">assoc</span> <span style="color: #b5cea8;">0</span> ename_list))
<span style="color: #6a9955;">;如果是圆形(圆心y+半径)</span>
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> ename_type (<span style="color: #569cd6;">cons</span> <span style="color: #b5cea8;">0</span> <span style="color: #ce9178;">"CIRCLE"</span>))
(<span style="color: #569cd6;">progn</span>
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> oo_py_fs_str <span style="color: #ce9178;">"n"</span>)
(<span style="color: #569cd6;">setq</span> oo_py_fs_num (* <span style="color: #b5cea8;">-1</span> (<span style="color: #569cd6;">cdr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">10</span> ename_list)))))
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> oo_py_fs_str <span style="color: #ce9178;">"w"</span>)
(<span style="color: #569cd6;">setq</span> oo_py_fs_num (<span style="color: #569cd6;">cdr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">10</span> ename_list))))
(<span style="color: #569cd6;">command</span> <span style="color: #ce9178;">"offset"</span> oo_py_dict ss_i_ename (<span style="color: #569cd6;">list</span> (<span style="color: #569cd6;">car</span> (<span style="color: #569cd6;">cdr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">9</span> ename_list))) (+ oo_py_fs_num (+ (<span style="color: #569cd6;">cdr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">10</span> ename_list)) (<span style="color: #569cd6;">cadr</span> (<span style="color: #569cd6;">cdr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">9</span> ename_list)))))) <span style="color: #ce9178;">""</span>)
)
)
<span style="color: #6a9955;">;如果是矩形(矩形中心点,y轴半高)</span>
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> ename_type (<span style="color: #569cd6;">cons</span> <span style="color: #b5cea8;">0</span> <span style="color: #ce9178;">"LWPOLYLINE"</span>))
(<span style="color: #569cd6;">progn</span>
<span style="color: #6a9955;">;对角点获取办法</span>
(<span style="color: #569cd6;">setq</span> p3_nth (+ (/ (- (<span style="color: #569cd6;">length</span> ename_list) <span style="color: #b5cea8;">15</span>) <span style="color: #b5cea8;">2</span>) <span style="color: #b5cea8;">14</span>))
<span style="color: #6a9955;">;p1起点</span>
(<span style="color: #569cd6;">setq</span> p1 (<span style="color: #569cd6;">list</span> (<span style="color: #569cd6;">setq</span> p1x (<span style="color: #569cd6;">cadr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">14</span> ename_list))) (<span style="color: #569cd6;">setq</span> p1y (<span style="color: #569cd6;">caddr</span> (<span style="color: #569cd6;">nth</span> <span style="color: #b5cea8;">14</span> ename_list)))))
(<span style="color: #569cd6;">setq</span> p3 (<span style="color: #569cd6;">list</span> (<span style="color: #569cd6;">setq</span> p3x (<span style="color: #569cd6;">cadr</span> (<span style="color: #569cd6;">nth</span> p3_nth ename_list))) (<span style="color: #569cd6;">setq</span> p3y (<span style="color: #569cd6;">caddr</span> (<span style="color: #569cd6;">nth</span> p3_nth ename_list)))))
<span style="color: #6a9955;">;矩形中心点</span>
<span style="color: #6a9955;">;矩形半高 (/ (- p1y p3y) 2)</span>
(<span style="color: #569cd6;">if</span> (> p1y p3y)
(<span style="color: #569cd6;">progn</span>
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> oo_py_fs_str <span style="color: #ce9178;">"n"</span>)
(<span style="color: #569cd6;">setq</span> oo_py_fs_num (* <span style="color: #b5cea8;">-1</span> (/ (- p1y p3y) <span style="color: #b5cea8;">2</span>))))
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> oo_py_fs_str <span style="color: #ce9178;">"w"</span>)
(<span style="color: #569cd6;">setq</span> oo_py_fs_num (/ (- p1y p3y) <span style="color: #b5cea8;">2</span>)))
(<span style="color: #569cd6;">if</span> (< p1x p3x)
(<span style="color: #569cd6;">command</span> <span style="color: #ce9178;">"offset"</span> oo_py_dict ss_i_ename (<span style="color: #569cd6;">list</span> (+ (/ (- p3x p1x) <span style="color: #b5cea8;">2</span>) p1x) (+ oo_py_fs_num p1y)) <span style="color: #ce9178;">""</span>)
(<span style="color: #569cd6;">command</span> <span style="color: #ce9178;">"offset"</span> oo_py_dict ss_i_ename (<span style="color: #569cd6;">list</span> (+ (/ (- p1x p3x) <span style="color: #b5cea8;">2</span>) p3x) (+ oo_py_fs_num p1y)) <span style="color: #ce9178;">""</span>)))
(<span style="color: #569cd6;">if</span> (> p3y p1y)
(<span style="color: #569cd6;">progn</span>
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> oo_py_fs_str <span style="color: #ce9178;">"n"</span>)
(<span style="color: #569cd6;">setq</span> oo_py_fs_num (* <span style="color: #b5cea8;">-1</span> (/ (- p3y p1y) <span style="color: #b5cea8;">2</span>))))
(<span style="color: #569cd6;">if</span> (<span style="color: #569cd6;">equal</span> oo_py_fs_str <span style="color: #ce9178;">"w"</span>)
(<span style="color: #569cd6;">setq</span> oo_py_fs_num (/ (- p3y p1y) <span style="color: #b5cea8;">2</span>)))
(<span style="color: #569cd6;">if</span> (< p1x p3x)
(<span style="color: #569cd6;">command</span> <span style="color: #ce9178;">"offset"</span> oo_py_dict ss_i_ename (<span style="color: #569cd6;">list</span> (+ (/ (- p3x p1x) <span style="color: #b5cea8;">2</span>) p1x) (+ oo_py_fs_num p3y)) <span style="color: #ce9178;">""</span>)
(<span style="color: #569cd6;">command</span> <span style="color: #ce9178;">"offset"</span> oo_py_dict ss_i_ename (<span style="color: #569cd6;">list</span> (+ (/ (- p1x p3x) <span style="color: #b5cea8;">2</span>) p3x) (+ oo_py_fs_num p3y)) <span style="color: #ce9178;">""</span>))
)
)
)
)
)
<span style="color: #6a9955;">;矩形判断-结束</span>
<span style="color: #6a9955;">;(subst (cons 8 (getvar "clayer")) (assoc 8 (entget (entlast))) (entget (entlast)))</span>
(<span style="color: #569cd6;">setq</span> temp_i (+ <span style="color: #b5cea8;">1</span> temp_i))
)
)
</div>
</div>写了个指定图层批量偏移到当前图层,但是只能偏移圆和4个点的矩形。图中这种圆角的图形就用不了, vla-offset可以不用取点 , 但是不知到怎么偏移到指定图层 ,很惆怅
;---------------------------------------------------------
vlisp好像也会被起点影响到..
(vla-put-Layer (Vlax-Ename->Vla-Object (entlast)) "图层名" )
这个功能不是CAD自带吗。偏移命令有子命令可以看一下 kucha007 发表于 2024-1-31 19:13
这个功能不是CAD自带吗。偏移命令有子命令可以看一下
那个"L" "C"的子命令有在使用 , 是自己在应用时发现 , 只有圆形和四四方方的矩形可以用 , 顶点一多 , 就不知道咋取封闭图形的内点了 uualice2020 发表于 2024-1-31 18:54
应用时发现 , vla偏移可以用负数内偏 , 但也会被图元起点影响 , 还是要取图形内的点 , 四边矩形我能用对角取点 , 图中那种圆角的矩形不知道怎么取对角 提供个思路:不知可行否?图形往两侧分别偏移,取得的新图形后比较二者的长度,长度短的为内偏移,长度长的为外偏移。 自己想的是 , 获取全部顶点 , 取最大xy和最小xy , 然后取他们的中点 , assoc只能取第一个 , 好像悟了 , 我去用 nth 和 if 判断试试获取全部顶点 fzlinrong 发表于 2024-2-1 09:12
提供个思路:不知可行否?图形往两侧分别偏移,取得的新图形后比较二者的长度,长度短的为内偏移,长度长的 ...
好像行欸 ! 我去查下求长度的命令 , 谢谢 as6424 发表于 2024-2-1 09:02
应用时发现 , vla偏移可以用负数内偏 , 但也会被图元起点影响 , 还是要取图形内的点 , 四边矩形我能用对 ...
dist 正值外偏负值内偏
(defun offent(ent dist / s0 s1)
(setq s0 (vlax-curve-getarea ent))
(vla-offset (vlax-ename->vla-object ent) dist)
(setq s1 (vlax-curve-getarea (entlast)))
(if (or (and (> dist 0)(< s1 s0))
(and (< dist 0)(> s1 s0)))
(progn (entdel (entlast))(vla-offset (vlax-ename->vla-object ent) (- dist)))
)(entlast)
)
页:
[1]