Jump to content

Recommended Posts

Posted (edited)

Hi again.
There's something I can't seem to find a clear answer to.
Maybe someone here can give me that answer.

I'm experimenting with the use of dynamic variables, created in real-time.
However, there's something that often makes me doubt whether it's advisable to use them.
And that's where my question lies:
If my code creates thousands of variables to avoid having to store data in a structure (lists or arrays)... when at the end of the code I cancel each of those variables with (set 'v nil), do the names of those variables really disappear from AutoCAD's symbol table, or only their content?

 

I know about the atoms-family function and that it returns the value of a symbol if it exists.
But the AI says that a negative response from atoms-family is not a guarantee that the symbol no longer exists in AutoCAD's internal symbol table.

 

Thanks in advance.

Edited by PGia
Posted (edited)

A couple of points to make:

  • The need to create thousands of variables on-the-fly typically points to poor program design; in almost every case, a list is more suitable.
  • Setting all such variables to nil at the end of the program does clear them from the document namespace, but consider that you are essentially creating & clearing global variables that are not local to the scope of your program, and as such, the variable names could potentially clash with identically named variables in the document namespace - this is generally considered bad practice.

 

Edited by Lee Mac
  • Like 3
  • Thanks 1
  • SLW210 changed the title to Dynamic variables
Posted

Thanks for your answer, @Lee Mac.

 

I've tried creating a list with the coordinates of almost 20,000 objects and managing it on the fly with assoc and subst.
However, the processing speed is quite slow.
For this reason, it occurred to me to try dynamic variables.

You're probably right.
But in your answer, you seem to concede a small possibility that using them might actually be worthwhile.
What do you mean by that?

Posted

@PGia

 

While it's very possible to create variables on the fly. I agree with Lee that it should not be a method to use, especially in the way you are wanting to use it.

 

That being said, I wrote the following many years ago, but hardly have ever used it for anything. It accepts an assoc. list of strings representing variables and the corresponding values, then creates a variable for each with the value.

The core of it is very simple (set (read "StrVar") value) 

;|==============================================================================
  Function Name: (pjk-dynsetvar)
  Arguments:
             assoclst = List of cons; an assoc list on the format:
                        (list (cons "<var name1>" <value1>) ... (cons "<Var namN>" <valueN>))
  Usage: (pjk-dynsetvar <Assoc. List>)
         Ex: (pjk-dynsetvar (list (cons "Cenpt" (list 1.0 1.0 0.0)) (cons "Radius" 0.25)))
  Returns:
     The results of the read list. You can verify the variables with the ! prefix:
     Command line Example:
     Command: (pjk-dynsetvar (list (cons "Cenpt" (list 1.0 1.0 0.0)) (cons "Radius" 0.25)))
     ((1.0 1.0 0.0) 0.25)
     Command: !Cenpt
     (1.0 1.0 0.0)
     Command: !Radius
     0.25
   Description:
      This function dynamically sets AutoLISP variables based on an association list of strings
      and values. The string keys represent the names of the variables that the values
      will be assigned to.
================================================================================|;
(defun pjk-DynSetVar (assoclst)
   (mapcar (function (lambda (x)(set (read (car x)) (cdr x)))) assoclst)
) ;; End Function (pjk-dynsetvar)

 

  • Like 1
  • Thanks 1
Posted
15 hours ago, PGia said:

I've tried creating a list with the coordinates of almost 20,000 objects and managing it on the fly with assoc and subst.
However, the processing speed is quite slow.

 

I Agree with Lee I'm interested in what your actually doing. take this for example. different approaches lisps that have a large range of run times. all basically doing the same thing.

mine still took over a min to run, but ronjonp came in with 4.07 sec.

  • theirs built an main list
  • processed that list into smaller lists.
  • once processed removed the items from each lists. (speeds up search)
Posted
4 hours ago, pkenewell said:

@PGia

 

While it's very possible to create variables on the fly. I agree with Lee that it should not be a method to use, especially in the way you are wanting to use it.

 

That being said, I wrote the following many years ago, but hardly have ever used it for anything. It accepts an assoc. list of strings representing variables and the corresponding values, then creates a variable for each with the value.

The core of it is very simple (set (read "StrVar") value) 

;|==============================================================================
  Function Name: (pjk-dynsetvar)
  Arguments:
             assoclst = List of cons; an assoc list on the format:
                        (list (cons "<var name1>" <value1>) ... (cons "<Var namN>" <valueN>))
  Usage: (pjk-dynsetvar <Assoc. List>)
         Ex: (pjk-dynsetvar (list (cons "Cenpt" (list 1.0 1.0 0.0)) (cons "Radius" 0.25)))
  Returns:
     The results of the read list. You can verify the variables with the ! prefix:
     Command line Example:
     Command: (pjk-dynsetvar (list (cons "Cenpt" (list 1.0 1.0 0.0)) (cons "Radius" 0.25)))
     ((1.0 1.0 0.0) 0.25)
     Command: !Cenpt
     (1.0 1.0 0.0)
     Command: !Radius
     0.25
   Description:
      This function dynamically sets AutoLISP variables based on an association list of strings
      and values. The string keys represent the names of the variables that the values
      will be assigned to.
================================================================================|;
(defun pjk-DynSetVar (assoclst)
   (mapcar (function (lambda (x)(set (read (car x)) (cdr x)))) assoclst)
) ;; End Function (pjk-dynsetvar)

 

 

Thank you very much, @pkenewell
I will try to make use of your code in my experiments.

However, I'm not sure whether I should consider my question answered: can AutoCAD's internal symbol table really be controlled from atoms-family?
Or is it possible that any symbol created during an AutoCAD session survives for as long as the session lasts, even if its value is nil?

I apologize if my question seems awkward or absurd, but I would really like someone to give me a clear answer about this.

Posted
1 hour ago, mhupp said:

 

I Agree with Lee I'm interested in what your actually doing. take this for example. different approaches lisps that have a large range of run times. all basically doing the same thing.

mine still took over a min to run, but ronjonp came in with 4.07 sec.

  • theirs built an main list
  • processed that list into smaller lists.
  • once processed removed the items from each lists. (speeds up search)

@mhupp: in principle, my goal is to make a comparison between processing speed when the information to be looked up is stored in a very large list versus when it is stored in variables.

I suppose that, as Lee or Pkenewell say, it is risky.
But I think that if atoms-family allows complete control of AutoCAD's symbol table, it is worth trying to test this coding method.

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...