See the top rated post in this thread. Click here

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Nesting Defun with Arguments and Variables

  1. #1
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Nesting Defun with Arguments and Variables

    All,

    I am trying to create a routine that will allow a user to setq for a value by using one of several functions, then pass that variable on to another function and complete a series of steps. My ultimate goal is to have users initiate commands, then create layers, adding a different discipline designator based on the command that they initiate. I know that I should set all the variables as local if possible, yet any time I try to do that, I don't get the result I need. I also tried to nest one function inside the other, but couldn't get it to run.

    The code below does run, but I am concerned about the fact that none of the variables are local.

    Any suggestions are appreciated.

    Thanks,

    Jeff

    Code:
    (defun c:settinga (/)
        (setq parta "V")
      )
    (defun c:settingb (/)
        (setq parta "VC")
      )
    (defun newlayer (parta /)
      (setq    partb '("-0001"
            "-0002"
            "-0003"
            "-0004"
            "-0005"
               )
      )
      (setq lcount (length partb))
      (setq lnext 0)
      (repeat lcount
        (setq partbcurrent (nth lnext partb))
        (setq final (strcat parta partbcurrent))
        (princ final)
        (setq lnext (+ 1 lnext))
      )
    )                    ;end defun
    (newlayer parta)

  2. #2
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,096
    Login to Give a bone
    1

    Default Re: Nesting Defun with Arguments and Variables

    Try this
    Code:
    (defun c:settinga (/)
      (newlayer "VC")
    )
    As you are processing a list, you could look into using a foreach loop instead of the repeat loop. It might even be quicker.
    Code:
    (defun newlayer	(parta / partb)
      (setq	partb '("-0001"	"-0002"	"-0003"	"-0004"	"-0005")
      )
      (foreach n partb
        (princ (strcat parta n "\n")) ;_ The "\n" is only for your testing to show each concatenation on a new line.
      )
    )
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  3. #3
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    Yes, I see now Opie. You have taken care of the problems with arguments vs. local variables and (if I can work through the nuances of foreach) given me a more efficient way to loop. Thanks for pointing me in the right direction.

    Regards,

    Jeff

  4. #4
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    I have a follow up question. In addition to creating the layer, I'd like to set the color, plot setting and description. The "foreach" function set a variable to a list and then performs a function for each item in the list. I am wondering if you can use a single "foreach" to step through each item in a series of lists. My "sketched" code looks like this:
    Code:
    (defun c:createlayersymbol ([arguments] [/ variables...])
      (setq acadobject (vlax-get-Acad-Object))
      (setq activedocument (vla-get-activedocument acadobject))
      (setq layertable (vla-get-layers activedocument))
      (setq	layer '("[LayerName001]"
    		"[LayerName002]"
    	       )
      )
      (setq	color '([Layer001Color]
    		[Layer002Color
    	       )
      )
      (setq	lt '("Layer001Linetype"
    	     "Layer002Linetype"
    	    )
      )
      (setq	plot '([Layer001PlotStatus]
    	       [Layer002PlotStatus]
    	      )
      )
      (setq	dscrip '("Layer001Description"
    		 "Layer002Description"
    		)
      )
      (foreach [name] layer
        ;I need to step through each element in the list "layer" and create a
        ;new layer with linetype (lt) plot setting (plot) and description (dscrip).
        ;Can I use one foreach for this?  How do I step through each element in these
        ;other lists?
        (setq newlayer (vla-add layertable [name]))
        (vla-put-color newlayer color)
        (vla-put-linetype newlayer lt)
        (vla-put-plottable newlayer plot)
        (vla-put-description newlayer dscrip)
      )
      )

  5. #5
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,096
    Login to Give a bone
    1

    Default Re: Nesting Defun with Arguments and Variables

    Why not set one list with a series of lists containing the necessary data for each layer?
    Code:
      (setq lstLayers (list
    		  '("Layer1" 1 "Continuous" t "First layer and plots")
    		  '("Layer2" 2 "Continuous" nil "Second layer and does not plot")
    		  '("Layer3" 3 "Continuous" t "Third layer and plots")
    		)
      )
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  6. #6
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    Well, that's a much simpler solution (wiping egg off my face). Is using CAR, CADR, CADDR, NTH, etc. to get each element from the list and feed it to the functions creating the layers the best approach or is there another more elegant solution you would recommend?

    Thanks Opie!

  7. #7
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,096
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    Those would be appropriate functions to use for this task.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  8. #8
    Member
    Join Date
    2016-01
    Location
    Wheat Ridge, Colorado
    Posts
    25
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    Opie,

    Thanks for all your patient guidance. I realize that this could probably still use some refinement, but I am posting this code for those who may be interested in doing something similar.

    Code:
    (defun c:settinga (/)
      (makeanewlayer "Prefix1-")
    )
    (defun c:settingb (/)
      (makeanewlayer "Prefix2-")
    )
    (defun c:settingc (/)
      (makeanewlayer "Prefix3-")
    )
    (defun makenewlayer ()
        (setq newlayer (vla-add layertable laynam))
        (vla-put-color newlayer laycol)
        (vla-put-linetype newlayer laylt)
        (vla-put-plottable newlayer layplt)
        (vla-put-description newlayer laydes)
      )
    (defun makeanewlayer (laypre / acadobjecct  activedocument
    		      laycol	   laydes	layertable
    		      laylt	   laynam 	layplt
    		      lstlay	   newlay
    		     )
      (setq acadobject (vlax-get-Acad-Object))
      (setq activedocument (vla-get-activedocument acadobject))
      (setq layertable (vla-get-layers activedocument))
      (setq lstlay (list
    		  	'("LayerName1"	10	"Continuous"	1	"Description Layer 1")
    			'("LayerName2"	4	"Continuous"	1	"Description Layer 2")
    			'("LayerName3"	132	"Continuous"	1	"Description Layer 3")
    			'("LayerName4"	8	"Continuous"	1	"Description Layer 4")
    			'("LayerName5"	95	"Continuous"	1	"Description Layer 5")
    		)
      )
      (foreach newlay lstlay
        (setq laynam (strcat laypre(nth 0 newlay)))
        (setq laycol (nth 1 newlay))
        (setq laylt (nth 2 newlay))
        (setq layplt (nth 3 newlay))
        (setq laydes (nth 4 newlay))
        (if	(tblsearch "LAYER" laynam)
          nil
        (makenewlayer)
      )
      )
      )

  9. #9
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,096
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    As you are utilizing subroutines for this, I would recommend you pass the layer information to your subroutine, "MakeNewLayer." Then, in your subroutine do the magic. This way, you do not have to assign the individual values to several variables outside the scope of the subroutine. You would then be able to make this subroutine a regular routine in your toolbox for future development.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  10. #10
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Nesting Defun with Arguments and Variables

    Quote Originally Posted by jeff.richards594944 View Post
    My ultimate goal is to have users initiate commands, then create layers, adding a different discipline designator based on the command that they initiate.
    This is a task many perform using Reactors / Custom commands... Please provide more information, so we can better offer help to achieving this goal.
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 13
    Last Post: 2013-05-23, 07:12 PM
  2. Help with Defun Argument
    By jeff.richards in forum AutoLISP
    Replies: 4
    Last Post: 2011-08-03, 04:23 PM
  3. DEFUN's "arguments"
    By BeKirra in forum AutoLISP
    Replies: 6
    Last Post: 2009-03-18, 09:31 PM
  4. assigning variables inside defun
    By jeff.richards in forum AutoLISP
    Replies: 3
    Last Post: 2008-11-26, 09:37 PM
  5. Explain the difference between defun and defun c
    By David van Erk in forum AutoLISP
    Replies: 6
    Last Post: 2007-06-08, 09:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •