Is there a way to gets this lisp to only changes points on layer "DOT" to circles and keep the new circles on the same layer?
|
Is there a way to gets this lisp to only changes points on layer "DOT" to circles and keep the new circles on the same layer?
Filter
(8 . ¨DOT)
Do the trick
Code:(defun c:pt2cir () (setq sset (ssget "_X" '((0 . "POINT")(8 . "DOT" ))) i 0) (if sset (repeat (sslength sset) (setq ent (ssname sset i)) (entmake (list (cons 0 "CIRCLE") (cons 8 "DOT") (cons 10 (cdr (assoc 10 (entget ent)))) (cons 40 10.0) ;circle size, change the 10.0 to whatever you want ) ) (entdel ent) (setq i (1+ i)) ) ) (princ) ) (prompt "\n TYPE PT2CIR at the command line")
Hi devitg,
A few comments if you would like to improve your codes.
- Always localize your variables to avoid any mix up with other programs on your system and to avoid hair pull.
The following line of codes:
is equal to:Code:(cons 10 (cdr (assoc 10 (entget ent))))
And you can remove the variable sset which is in front of the 'IF' function and just move the 'IF' function to be for eg: (if (setq sset (... etcCode:(assoc 10 (entget ent))
Hope this helps.
Eg:
Code:(defun c:pt2cir ( / sset i ent) (if (setq sset (ssget "_X" '((0 . "POINT") (8 . "DOT")))) (repeat (setq i (sslength sset) ) (setq ent (ssname sset (setq i (1- i)))) (if (entmake (list '(0 . "CIRCLE") '(8 . "DOT") (assoc 10 (entget ent)) (cons 40 10.0))) (entdel ent) ) ) ) (princ) ) (prompt "\n TYPE PT2CIR at the command line")
I thought maybe I would throw in an activeX example.
I would also like to mention to you all that coding has other priorities than the fewest number of characters or speed.
I would suggest you also consider stability. LISP has always been an unstable language that crashes easily.
I have found placing errortraps around expressions that may cause a problem (like a locked layer for example) helps.
Function syntax that looks like a try catch works better (IMO)
I also try to not nest statements (although that is what lisp is good at), so I can test each expression to make sure it was
successful before using the value in the next expression.
I also absolutely do not like short non-descriptive variable names... X is not a variable name unless used in a lambda expression.
I know the reddick naming convention has fallen out of favor, but to me coding without clear variable names is just sloppy code.
If you cannot figure out what an expression is doing right away, you need more comments and clear variable/function naming.
I wrote a project for a customer that had over 10000 lines of code and it was like 2 meg, so size is never a problem with LISP.
OK I will get down off my soap box...
Code:;___________________________________________________________________________________________________________| ; ; Command line Function to convert points to circles (including in block definitions) ;___________________________________________________________________________________________________________| (defun C:PointToCircle (/ objActiveDocument objBlock objPoint strBlockName strSearchBlocks) (initget 0 "Y N") (setq objActiveDocument (vla-get-activedocument (vlax-get-acad-object))) (if (or (setq strSearchBlocks (getkword "\nChange Points in Blocks Too? <Y> ")) (not strSeachBlocks) ) (vlax-for objBlock (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) (setq strBlockName (vla-get-name objBlock)) (if (or (= strSearchBlocks "Y") (= (substr strBlockName 1 1) "*") ) (vlax-for objItem objBlock (if (= (vla-get-objectname objItem) "AcDbPoint") (and (setq lstInsertion (vlax-get objItem "coordinates")) (errortrap '(setq objCircle (vlax-invoke objBlock "addcircle" lstInsertion 1.0))) (errortrap '(vla-put-layer objCircle (vla-get-layer objItem))) (errortrap '(vla-delete objItem)) ) ) ) ) ) ) (errortrap '(vla-regen objActiveDocument 1)) ) ;___________________________________________________________________________________________________________| ; ; Function to trap expression errors ;___________________________________________________________________________________________________________| (defun ErrorTrap (symFunction / objError result) (if (vl-catch-all-error-p (setq objError (vl-catch-all-apply '(lambda (XYZ)(set XYZ (eval symFunction))) (list 'result)))) nil (or result 'T ) ) ) (princ "!") (vl-load-com)
Last edited by peter; 2017-10-10 at 01:02 AM.
AutomateCAD
Hi Peter, tharwat, thanks for your tips.
First all , it was my fault not to mention that the lisp it is not mine.
Even I do not know where I got it .
Maybe it was here.
http://www.cadtutor.net/forum/showthread.php?
13470-Converting-an-object-into-an-other
Peter I used your errortrap defun , and I saw that it had a la DEBUG mode
There is any change to it.?Code:;; BY Peter Jamtgaard-01 (DEFUN ERRORTRAP (SYMFUNCTION / OBJERROR RESULT) (IF (VL-CATCH-ALL-ERROR-P (SETQ OBJERROR (VL-CATCH-ALL-APPLY '(LAMBDA (X) (SET X (EVAL SYMFUNCTION))) (LIST 'RESULT)))) (PROGN (IF DEBUG (PRINC (STRCAT "\n" (VL-PRINC-TO-STRING (VL-CATCH-ALL-ERROR-MESSAGE OBJERROR)) "\nWhile evaluating the expression: " (VL-PRINC-TO-STRING SYMFUNCTION) "\n" ) ) ) NIL ) (IF RESULT RESULT 'T) ) ) ;;;(while (not (errortrap (quote (setq region (car (vlax-invoke model "addregion" (list poly-reg ))))))) ;;; (princ "\nzError: Invalid polyline ") ;;; ; Call your function for selecting the polyline here ;;;)
And as another way to error trap I use a RECURSIVE defun
I have to dig in my lsp to find a sample .
Thanks again.
there is no real change to the errortrap function, I just removed the debug mode because I don't use it and it makes the code more complicated.
The vlax-catch-all-apply expression requires you to pass it a list and accidentally I found out that you can pass it the result as a list.
This was fortuitous because I write code that checks for success by the expression either returning a nil or T/Vaue. nil for failure.
If you wrap all of your expressions with an "and" expression the function will continue until it fails and drop out very similar to a try catch in .NET.
In lisp would look like this.
Code:(if (and expression1 expression2 .... ) return value catch expressions )
AutomateCAD
Thank you for sharing this lisp! I was wondering if there is anyway to change it so that when I convert the points they can stay with the layers assigned to them. I mark utilities and there are several different ones, when I insert the command it changes all points to the current layer being used. i have tried to turn off some of the points before inserting it but it still does it. Thank you again and I hope there is an easier way than manually changing them all.
Hi Timothy,
The example C:PointToCircle I posted 4 replys above maintains the layer between the point and the new circle.
There is a downloadable file in that response I added for you too.
Peter
AutomateCAD