Jump to content

Recommended Posts

Posted (edited)

I'm trying to run the command "_pline" of the selected line type through Lisp, which
would be entered onto a specific layer, and upon completion the current one would be returned
layer. I'm a novice in programming, I try but in vain. Maybe someone can fix it?
I would be very grateful.
 

(defun C:gass ( / cl )
(if (not (tblsearch "LTYPE" "GASs")
(command "_-linetype" "_l" "GASs" "truba.lin" "_s" "GASs" "")
(if (/= (getvar "CELTYPE") "GASs")(setvar "CELTYPE" "GASs"))
(setq cl (getvar "CLAYER" "0"))
(command "_-layer" "_m" "PIPELINES" "")
(command "_pline")
(while (= (getvar "cmdactive") 1)(command pause))
(setvar "CLAYER" cl))))
(C:gass)
;;
;;
(defun C:feces( / cl )
(if (not (tblsearch "LTYPE" "Sewerage")
(command "_-linetype" "_l" "Sewerage" "truba.lin" "_s" "Sewerage" "")
(if (/= (getvar "CELTYPE") "Sewerage")(setvar "CELTYPE" "Sewerage"))
(setq cl (getvar "CLAYER" "0"))
(command "_-layer" "_m" "SEWERAGE" "")
(command "_pline")
(while (= (getvar "cmdactive") 1)(command pause))
(setvar "CLAYER" cl))))
(C:feces)

 

Edited by SLW210
Added Code Tags!
Posted

Please use Code Tags (<> in the editor toolbar) in the future.

Posted (edited)
(defun c:gass ( / cl clt ltypename layername)

	(setq ltypename "GASs")
	(setq layername "PIPELINES")

	(setq clt (getvar "CELTYPE")) ; Get active linetype
	(if (not (tblsearch "LTYPE" ltypename)) ; If linetype does not exist
		(command "_-linetype" "_l" ltypename "truba.lin" "_s" ltypename "") ; Create it
	)
	(if (/= (getvar "CELTYPE") ltypename) ; If linetype is not active
		(setvar "CELTYPE" ltypename) ; Activate it
	)

	(setq cl (getvar "clayer")) ; Get active layer
	(if (not (tblsearch "LTYPE" layername)) ; If layer does not exist
		(command "_-layer" "_m" layername "") ; Create it
	)
	(if (/= (getvar "CLAYER") layername) ; If layer is not active
		(setvar "CLAYER" layername) ; Activate it
	)

	(command "_pline") ; Start command
	(while (= (getvar "cmdactive") 1) ; As long as command is active, pause
		(command "\\")
	)

	(setvar "CELTYPE" clt) ; Activate it
	(setvar "CLAYER" cl) ; Reset layer to what it was before this function
)

I've added some comments, hopefully this makes it more clear what is happening.

And it would make a nice challenge to fix the second one yourself 😁

Edited by dexus
  • Like 1
Posted
(defun c:gass ( / var vals)
  (setq var (list "CELTYPE" "CLAYER")
        val (mapcar 'getvar var)
  )
  (if (not (tblsearch "LTYPE" "GASs")) ;idk if this will error if existing so left this check in but could prob take it out also
    (command "_-linetype" "_l" "GASs" "truba.lin" "_s" "GASs" "")
  )
  (setvar 'celtype "GASs")  
  (entmake '((0 . "LAYER") (100 . "AcDbSymbolTableRecord") (100 . "AcDbLayerTableRecord") (2 . "PIPELINES") (70 . 0) (62 . 1) (290 . 0))) ;change 62 to the coler you want
  (setvar 'clayer "PIPELINES")
  (command "_pline") 
  (while (= (getvar "cmdactive") 1) 
    (command "\\")
  )
  (mapcar 'setvar var vals)
  (princ)
)

 

@dexus added a mapcar to set an return variables

Maybe someone can correct me but I don't check for current layer just issue the change.

Also try not to use command when possible entmake for the layer.

 

@boretskiy if statements in lisp only run the first two lines of code.

 

(if condition
  then-part
  [else-part]
)

 

to run multiple lines of code you would have to wrap them with (progn

You also need to match up ( ) for the code to even load. I recommend getting notepad++ it will color code things for you so its easier to track down

 

(defun C:gass ( / cl )
  (if (not (tblsearch "LTYPE" "GASs"))
    (command "_-linetype" "_l" "GASs" "truba.lin" "_s" "GASs" "") ;if true do this
    ;false isn't needed to run
  )
  ...

(defun C:gass ( / cl )
  (if (not (tblsearch "LTYPE" "GASs"))
    (command "_-linetype" "_l" "GASs" "truba.lin" "_s" "GASs" "") ;if true do this
    (progn
      (prompt "\nGASs line Type already in the drawing")
      (setvar 'celtype "GASs")
    )
  )
  ...

 

Also don't have  commands outside of functions (C:feces)  (C:Gass) this calls the command when the lisp is loaded. Seeing its needs user input it would just wait their until you cancel the command. its not needed to close a function.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...