General questions about AutoLisp functions and OOP

Anonymous

General questions about AutoLisp functions and OOP

Anonymous
Not applicable

 

 

Hi!

I am new to AutoLisp.

I have a few questions:

1)Is auto AutoLisp object oriented?
2) Can I define functions in main program?

3) not how do I call function from another function?

 

Thank you in advance

0 Likes
Reply
Accepted solutions (1)
2,129 Views
14 Replies
Replies (14)

Kent1Cooper
Consultant
Consultant

Search this Forum for things like "tutorials" or "learn AutoLisp", and you will find many website links, book titles, and so on that can get you going.

Kent Cooper, AIA

Anonymous
Not applicable

@Kent1Cooper!

Thanks for reply!

Thanks for useful info.


But could you please answer my questions?

 

Thanks!

0 Likes

Kent1Cooper
Consultant
Consultant

I'm afraid I don't completely understand the questions, but here goes....

 

1)  I'm not sure what "object oriented" really means.  Other people more familiar with the term would be able to help more than I can.  AutoLisp can certainly do many things that are not specific to "objects," so the answer may be No, but many of its functions do work on drawing entities, if that means that the answer is [partially?] Yes.  Some drawing entities represent "objects," but many do not, if that suggests an answer.

2)  What do you mean by "in main program"?  In AutoCAD, yes, you can define functions [that's one of the main things AutoLisp is used for], if that's what you mean, but usually by way of something else, such as a text editor outside AutoCAD, or the Visual Lisp console [VLIDE], which operates from inside AutoCAD but has its own distinct way of working, so it's not really "in the main program" in the same way as other operations.  If you mean "Can I add a function into the 'core' AutoCAD program itself?", the answer is No, but you can make your own functions and commands, and have them automatically loaded in every drawing, which should be effectively the same.

3)  Does the word "not" belong in that question?  Assuming it should not be there, the short answer is Yes, but by "function" do you mean AutoLisp's internal functions themselves, or a function defined using AutoLisp?  Yes, you can call AutoLisp functions from within other functions -- a very simple example:

 

(setq Point1 (getpoint "\nPick point: "))

 

And you can call functions defined using AutoLisp [with the (defun) function] from within other functions, by putting the defined function's name in parentheses.

 

Go to the Customization Guide and some of the tutorials and website links that you will find with a Search as suggested before, and put some time into it, and your basic questions will be answered.

Kent Cooper, AIA

Anonymous
Not applicable

Thanks for reply!

I will explain my questions in more details here

 

+++++++++++++++++++++++

............1)  I'm not sure what "object oriented" really means.

 

Here is definition of object oriented programming(OOP)  https://en.wikipedia.org/wiki/Object-oriented_programming

 

Saying simply

 

 

 

a)If I draw a box, can I treat it like an object?

I mean can I change its properties (like in properties menu) by specifying the Width  parameter in my function?

 

b) If have helix and I used sweep tool, is there way to treat it as an object. Can I specify or change its height, like I use stretch command ?

 

+++++++++++++++++++++++++++=

 

 

2)  What do you mean by "in main program"?  

 


I mean the following:

 

Let's say I have this function

(defun C:function1 (/ pl p2 p3 p4)...... )

 

is there way to define function inside this function and call it when I need it

 

 

3) Yes, you answered my question.

 

 

 

Thank you!

0 Likes

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....

1) 

a)If I draw a box, can I treat it like an object?

I mean can I change its properties (like in properties menu) by specifying the Width  parameter in my function?

 

b) If have helix and I used sweep tool, is there way to treat it as an object. Can I specify or change its height, like I use stretch command ?

 

2)  What do you mean by "in main program"?  


I mean the following:

 

Let's say I have this function

(defun C:function1 (/ pl p2 p3 p4)...... )

 

is there way to define function inside this function and call it when I need it

 

....

1)  There are different ways to draw many things, but some of them would be considered objects in that way.  a) A box drawn as a 1-unit cube and defined as a Block can be Inserted at whatever scale factors you need for its size, and the scale factors can be changed in the Properties box.  PRESSPULL can be used to adjust the size of various kinds of things.  Dynamic Blocks are another way to get changeable properties.  In some of the overlay discipline-specific programs, there are special kinds of objects not available in plain AutoCAD -- for example in the Architectural overlay, there are walls that know they are walls, and windows that know they are windows, and when you put a window into a wall, the wall automatically has the appropriate hole cut in it, and if you move the window, the hole in the wall moves with it, etc.  b)  A helix can be played with in that way, but I'm not sure about what you get from Sweeping something along it [I'm at my older-version location at the moment, without either Helix or Sweep commands available].  If the result is a 3D Solid as it is from Extruding something along it, that would not be adjustable as you describe, but someone with a newer version could answer for a Sweep result.

 

2)  Yes -- this is done all the time, with the internally-defined function commonly referred to as a "subroutine."

 

Kent Cooper, AIA

dgorsman
Consultant
Consultant

LISP is not object oriented, but there are some functions provided for accessing methods and properties of COM/ActiveX objects.  Otherwise its a lot like F# in structure, being functional rather than imperative in style.

 

LISP cannot create arbitrary drawing objects, it can only work with already established AutoCAD objects.

 

AutoCAD 3D solids are not parametric, so they cannot be easily modified.

 

There is no concept of scope in LISP - everything is "global" with the exception that functions can localize some variables.  That includes functions, so creating a (defun foo ( arg1 arg2 ) ... ) function would be available inside all other functions as (foo "TEST" 1) using appropriate arguments.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Anonymous
Not applicable

Guys!

 

Thanks for replies!

 

I want to tell you my story regarding my AutoLisp adventure.

 

I am drawing my guitar.

 

The guitar fretboard has different space sizes between the frets.
(see the attached image)

 

I have an equation for calculating the steps between frets.
(see the attached file)

 

I want to write AutoLisp program

 

that will draw an array of frets with the variable steps.

 

I hope that with God's help and with help of participants of this forum I will win this challenge.
:)))

 

0 Likes

Kent1Cooper
Consultant
Consultant

If you don't want to just copy the fret Lines and scale them as I suggested when I posted that drawing, it can certainly be automated.  Do you need to do this often enough that a routine is really worthwhile?  Even if not, just as an exercise, here is one way.  I did the drawing by repeatedly setting the Offset distance for each fret and Offsetting the Bridge Line each time, but for an automated routine it seemed a little easier to do it by repeated Copying, though Offset could certainly be used instead.

 

(defun C:FRETS (/ fret bridge neck b1 n1 len ang i)
  (defun fret (n) (/ len (expt (expt 2 (/ 1.0 12)) n)))
  (setq
    bridge (car (entsel "\nSelect BRIDGE Line: "))
    neck (car (entsel "\nSelect NECK-end Line:"))
    b1 (vlax-curve-getStartPoint bridge)
    n1 (vlax-curve-getClosestPointTo neck b1 T)
    len (distance b1 n1)
    ang (angle b1 n1)
  ); setq
  (repeat (setq i 20); <-- can do more than 20 if desired
    (command "_.copy" bridge ""
      "_none" b1
      "_none" (polar b1 ang (fret i))
    ); command
    (setq i (1- i))
  ); repeat
  (princ)
); defun
(vl-load-com); [if needed]
(prompt "\nType FRETS to draw frets between bridge and neck-end Lines.")

That requires the Bridge and Neck-end Lines to be drawn already, and to be in the proper relationship to each other.  It could also be done in a way that would draw those Lines within the routine, asking the User only to specify the distance between them, and if you like a direction and/or location.  And of course it could be made to verify that Lines were actually selected and properly aligned, and that the Bridge Line is not on a locked Layer, use [and make if necessary] a specific Layer, etc.

 

Kent Cooper, AIA

Anonymous
Not applicable

@Kent1Cooper !

 

Thanks for your last post!

I am still not so good with AutoLisp,

but you gave me the direction on what I need to learn, what commands I should learn.

 

This is very precious knowledge.

 

Thank you!

0 Likes

Kent1Cooper
Consultant
Consultant

s.shivaprem wrote:

....

Thanks for your last post!

I am still not so good with AutoLisp,

but you gave me the direction on what I need to learn, what commands I should learn.

....


You're welcome.  A good exercise for you, that should not be too difficult for a beginner, would be to figure out how to make it do the frets using Offset instead of Copy.

Kent Cooper, AIA

Anonymous
Not applicable

OOP Programming has several aspects that need support to be utilized in AutoLisp.  You will need to create an OOP Framework that at some point in time would probably require use of the dreaded eval command to do this easily.  I am writing this because I am considering writing such a framework.

 

Basic OOP needs.

 

Terminology: For the sake of this prose...a "member" refers to either a property or method of the object/class/(structured list) and the terms "base" and "derived" indicate the relationship between objects

 

Encapsulation: The ability to combine both properties and methods accessible via a single object/variable. 

 Implementation: Create an object (structured list) via a new memory allocation that can contain what that object has (properties or attributes) and what it does (methods or functions)...i.e. it's members.
  AutoLisp supports this via specially constructed lists that are keyed (assoc key list) according to the names of the objects members.

 

Inheritance: The ability to access/share members between objects that are related (forex, a bird being the base class/object and an eagle or penguin being derived objects.

 Implementation: Use a standard additional "default" member to all objects that are derived from others to act as a portal to invoke the member of the base object.  Create a hierarchy to call or access member methods and properties of another object that acts as the "base" class.

 

Protection/Isolation of the objects members: The ability to allow some objects to allow/inhibit/hide members of one object to another object.

 Implementation: Add a bit flag that you can discern whether or not the object member should be exposed to other objects for both inheritance and referencing.

 

This is a tough one, but it is doable...you better get funding first or share the load with others via an open source project.

 

I would personally opt for a code generator application written in whatever OOP language you like that would create the lisp files on demand using JIT (on-the-fly) generation of the AutoLisp functions you would need.

 

I am still unsure if I will create this framework.  My language of choice would be either VB, C#(.net), or C++ for the engine and AutoLisp as the caller.  If you think of your code as data that not only houses values, but also can manipulate those values...then you have a good start.

 

Best of luck...

Rob

devitg
Advisor
Advisor

If your first post  shall be this one.

 

Often user ask HOW to do , not knowing HOW. 

 

If you instead of HOW to do, put WHAT you need to, and what you have it will easy to help. . 

 

And the distance between Frets is some kind of logarithmic formula.

 

Some years ago I do some like it , for a  luthier . 

 

I will search for it 

 

 

devitg
Advisor
Advisor

Erased by devitg 

 

 

martti.halminen
Collaborator
Collaborator

 

If you want to build such a framework as a programming project, feel free.

 

For anybody just wanting to do OOP in Lisp a far simpler way would be to use Common Lisp, which has a very flexible object system (CLOS).

Getting CL and AutoLISP to communicate is pretty simple, though all CL results would need to be reduced to AutoLisp datatypes.

 

--