乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 93|回复: 9

[编程交流] How's my code??

[复制链接]

35

主题

140

帖子

108

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
177
发表于 2022-7-5 19:58:18 | 显示全部楼层 |阅读模式
I wrote a lisp to draw a pipe break.
 
Is it coded efficent?
 
I'd like to have it check if the layer is frozen, if it is thaw it. (same with if its off)
 
  1. (defun c:pb (/ olayer)(setq olayer (vlax-variant-value (vla-getvariable (vla-get-activedocument                        ;remembers current layer(vlax-get-acad-object)) "clayer")))(setq pt1 (getpoint "\n Select First Point: "))(setq pt2 (getpoint "\n Select Second Point: ") dist (distance pt1 pt2) ang (angle pt1 pt2) pt3 (polar pt1 ang (/ dist 2.0)));end setq(if (not (tblsearch "layer" "c_gen_Dimn"))                                                        ;check to see if layer exist if not create it(command "_.layer" "m" "c_gen_Dimn" "c" "132" "" "")                                                 ;create layer(setvar "clayer" "c_gen_Dimn")                                                                        ;set layer new layer current current)(setvar "plinewid" 0.0)(command ".pline" pt2 "arc" "angle" "80" pt3 pt1 "angle" "-80" pt3 "")(vla-setvariable (vla-get-activedocument (vlax-get-acad-object)) "clayer" olayer)                ;return to previous layer(princ))
回复

使用道具 举报

12

主题

395

帖子

384

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
60
发表于 2022-7-5 20:11:29 | 显示全部楼层
From what I see there is nothing wrong with your code. Programming is an art. With codes like you have, there really isn't a wrong way to do it if it works. People like Lee-Mac will write a code 3 different ways just because it's possible. There are a few things you can do differently, but what you have seems to be fine.
回复

使用道具 举报

12

主题

395

帖子

384

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
60
发表于 2022-7-5 20:12:17 | 显示全部楼层
For instance, you could have gotten your midpoint using this:
  1. (defun mid (p1 p2) (list   (/ (+ (car p1) (car p2)) 2.0)   (/ (+ (cadr p1) (cadr p2)) 2.0) ))
But your way also works. You could also Get and Set your layers variables thus:
  1. (setq olayer (getvar 'clayer))(setvar 'clayer olayer)
Which you use at some points in your code. But again, using vla is not a bad option. Lastly, some people find that using "command line cop-outs" is less efficient. So if you wanted to create your layer or polyline using another method, you could.
回复

使用道具 举报

35

主题

140

帖子

108

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
177
发表于 2022-7-5 20:20:35 | 显示全部楼层
I'm a newb when it comes to coding, but I desperately want to learn.
 
I searched and found the vla for remembering layers and applied it to my code. I didnt write it myself
 
I like the setvar options better, I understand it better. The vlax seems to go through alot of steps just to remember 'clayer.
回复

使用道具 举报

12

主题

395

帖子

384

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
60
发表于 2022-7-5 20:30:19 | 显示全部楼层
Everyone starts somewhere. Most of the time when you are trying to write code you find pieces of it already written, and you can just apply it to your code.
回复

使用道具 举报

26

主题

1495

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
118
发表于 2022-7-5 20:32:39 | 显示全部楼层
A robust SteLayer call needs to include a god bit off error trapping
 
  1. ;++++++++++++ Make Layer Current +++++++++++++++++++++++++++++++++(defun SetLayer (name / ldef flag) (cond ((or (not name)    (not (snvalid name)))(princ "\nBad Aurgment Passed To SetLayer - ")(prin1 name)(exit))) (command "_.LAYER") (if (not (tblsearch "LAYER" name))     (command "_Make" name)     (progn      (setq ldef (tblsearch "LAYER" name)     flag (cdr (assoc 70 ldef)))      (and (= (logand flag  1)  1)    (command "_Thaw" name))      (and (minusp (cdr (assoc 62 ldef)))    (command "_On" name))      (and (= (logand flag  4)  4)    (command "_Unlock" name))      (and (= (logand flag 16) 16)    (princ "\nCannot Set To XRef Dependent Layer")    (quit))      (command "_Set" name))) (command "") name)
 
The (exit) call is totally up to you and your style of coding.
 
-David
回复

使用道具 举报

2

主题

389

帖子

387

银币

初来乍到

Rank: 1

铜币
10
发表于 2022-7-5 20:42:14 | 显示全部楼层
I agree that you have to start somewhere.  If you want to learn, I've no  doubt you will.  Going through the alternate versions here should help,  though some will be harder to understand than others.  I'm sure if you ask, someone will explain the reasoning behind the code.
 
I applaud your use of comments in your routine.  That can be very important when you want to edit it later, but have long forgotten what you were thinking at the time you were writing the program.
 
One thing I would  point out here is that it is generally good practice to keep your variables local  to a routine, which is what the slash mark is for on your top line.  This way you don't have to worry about their values being overwritten anywhere, or overwriting any globals from another routine.  To make them local you would simply include all the other variable names after clayer.  Something like this:
  1. (defun c:pb (/ olayer pt1 pt2 pt3 dist ang)
However, this does mean that you cannot easily check their values in the drawing session after the routine has run.
 
Happy LISPing!
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 20:49:35 | 显示全部楼层
I aggee with getvar shorter than VL, I would suggest that you consider a library style of common functions like "does layer exist" by making it a defun like David's and auto load it, it is very useful that all your code can have a quick check layer which only needs 1 line (setlayer layname)
回复

使用道具 举报

12

主题

395

帖子

384

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
60
发表于 2022-7-5 20:57:46 | 显示全部楼层
I totally agree with BIGAL. If you do have a sub function (like the mid function I posted earlier) it is generally good practice to put it inside your main function. Hopefully I don't lose you here, but... If you have a sub function inside your main function it acts as a "private function" ie you can't call it from a completely separate program. if you have it in your routine outside your main program it will act as a global function, which can be called form any other function. If you decide to alter the function on a program to fit your needs, it may not work on other functions that you call it.
 
Hope that's not confusing.
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2022-7-5 21:01:38 | 显示全部楼层
Just following on the library lisp can be any name and there are various ways to autoload it, one of mine has 82 defuns in it. In a particular suite there are 8  defuns used every time in the majority of the lisps, these all being in the "Library". There are so many good defuns out there I would start with some common ones like the entmakes LINE PLINE CIRC ARC, it also means your code is consistent in the methods used. Make sure you add good comments about what they do and leave original authors etc.
 
I am about to rewrite all my plot routines 12 off, into one and have a dcl front end so it will have common used defuns.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-12 01:07 , Processed in 0.393341 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表