Jump to content

ABC's of Autolisp by George Omura : Data type convertion is needed


samifox

Recommended Posts

hi

its my first day in "ABC's of Autolisp by George Omura" class. i tried to use this code but its not working. im sure its data type mismatch.

 

i simply want to program to draw a line from pt1 to pt2

 

(defun C:test()
 (setq pt1 (list (getpoint)))
 (setq pt2 (list (getpoint)))
 (command "Line" pt1 pt2)
 
   );end defun

 

Thanks

Link to comment
Share on other sites

thanks GP

 

hy i cant use the CAR and CADR functions to retrieve list component obtained with getpoint function?

Link to comment
Share on other sites

You could just incorporate the pause function:

 

(defun c:TEST ()
 (command "._line" pause pause "")
 (princ)
)

 

... But this may cause undesired behavior following a right click being entered in lieu of a point specification. This adaptation is a bit less prone to this undesired behavior:

 

(defun c:TEST ()
 (command "._line")
 (princ)
)

 

... If the requirement is to use two variables, then consider using local in lieu of global variables, and avoid potential errors in the Command call by using an If statement:

 

(defun c:TEST (/ point1 point2)
 (if (and (setq point1 (getpoint "\nSpecify start point: "))
          (not (initget 32))
          (setq point2 (getpoint point1 "\nSpecify end point: "))
     )
   (command "._line" point1 point2 "")
   (prompt "\n** Invalid point ** ")
 )
 (princ)
)

Link to comment
Share on other sites

[w]hy i cant use the CAR and CADR functions to retrieve list component obtained with getpoint function?

 

Here is an example to consider:

(defun c:getpt ( / pt )
   (if (setq pt (getpoint "\nPick a point, any point: "))
       (princ
           (strcat
               "\nX = " (rtos (car pt))
               "\nY = " (rtos (cadr pt))
               "\nZ = " (rtos (caddr pt))
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

i saw some using command "LINE","._LINE" what is the difference? when you say "IF" and "AND" ,logically is like making nested IF expressions. right?

Link to comment
Share on other sites

... when you say "IF" and "AND" ,logically is like making nested IF expressions. right?

 

In your original code in the OP, were you to right click instead of select the second point (for example), you would experience undesired behavior.

 

In this circumstance, the If statement is testing for the two valid points needed to supply the following Command call the arguments need to complete the command successfully.

 

The And statement simply allows for more than one non-Nil returned value to be required, in this case being point1, and point2 local variables, in order for the If statement's test expression to pass.

Link to comment
Share on other sites

In addition to BlackBox's explanation:

 

When you say "IF" and "AND", logically is like making nested IF expressions. right?

 

Correct, the following expression...

([color=blue]if[/color]
   ([color=blue]and[/color]
[color=green]        <test-expression-1>
       <test-expression-2>
       <test-expression-3>[/color]
   )
[color=red]    <then-expression-1>[/color]
[color=purple]    <else-expression-1>[/color]
)

...could alternatively be written:

([color=blue]if[/color] [color=green]<test-expression-1>[/color]
   ([color=blue]if[/color] [color=green]<test-expression-2>[/color]
       ([color=blue]if[/color] [color=green]<test-expression-3>[/color]
[color=green][color=red]            <then-expression-1>[/color]
[color=purple]            <else-expression-1>[/color][/color]
       )
[color=green] [color=purple]       <else-expression-2>[/color][/color]
   )
[color=green] [color=purple]   <else-expression-3>[/color][/color]
)

Here, each nested if statement forms the then-expression for the previous if statment.

 

Though, where additional else-expressions are not required, I'm sure that most would agree that the use of an and statement is far more readable and concise than multiple nested if statements.

Link to comment
Share on other sites

The same could be expressed as:

 

[b][color=BLACK]([/color][/b]cond [b][color=FUCHSIA]([/color][/b][b][color=NAVY]([/color][/b]and <test-expression-1>
           <test-expression-2>
           <test-expression-3>[b][color=NAVY])[/color][/b]
       <else-expression-3>[b][color=FUCHSIA])[/color][/b]
     [b][color=FUCHSIA]([/color][/b][b][color=NAVY]([/color][/b]and <test-expression-1>
           <test-expression-2>[b][color=NAVY])[/color][/b]
      <else-expression-2> [b][color=FUCHSIA])[/color][/b]
     [b][color=FUCHSIA]([/color][/b]<test-expression-1>
      <else-expression-1>[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

 

-David

Link to comment
Share on other sites

why i cant use the CAR and CADR functions to retrieve list component obtained with getpoint function?

 

In case it was missed, the answer is that you CAN use them, but the point is already a list. You made it an embedded list, and that doesn't work as straightforwardly.

Link to comment
Share on other sites

  • 2 weeks later...

hi

 

for what reasom the main() is not calling to getTotalLength()?

(defun c:main ()
 (setq totLng getTotalLength)
 (princ)
 )
(defun getTotalLength ( / ptst pten d n6 n8 n1 n2 n k stpt enpt )
 (princ "getting the total length of the line")
 (setq ptst (getpoint "\nStart point of line : "))
 (setq pten (getpoint ptst "\nEnd point of line : "))
 (setq ptst (trans ptst 1 0) pten (trans pten 1 0))
 (setq d (distance ptst pten))
 )
(defun getSegmentLength ()
 (princ)
 )

 

Thanks

Shay

Link to comment
Share on other sites

To solve your error:

(defun c:main ( / totLng )
   (setq totLng (getTotalLength))
   (princ)
)
(defun getTotalLength ( / ptst pten d )
   (princ "getting the total length of the line")
   (setq ptst (getpoint "\nStart point of line : "))
   (setq pten (getpoint ptst "\nEnd point of line : "))
   (setq ptst (trans ptst 1 0)
         pten (trans pten 1 0)
   )
   (setq d (distance ptst pten))
)
(defun getSegmentLength ( )
   (princ)
)

However, for what the code is doing, it could be simply:

(defun c:main ( / totLng )
   (setq totLng (getdist "\nPick Distance: "))
)

Link to comment
Share on other sites

hi

 

im learning how too access element in lists , why its so hard?

 

i tried to write a program that assign 1-10 to a veriable and than prints all elements one by one. of course i end up with ugly error

 

how to do it right?

 

;**accessing single element of a list page 17*;
(defun c:abcd()
 (setq pt1(list 1 2 3 4 5 6 7 8 9 0))
 (princ (car pt1))
 (princ (cdar '(pt1)))
 )

 

Thanks

Shay

Link to comment
Share on other sites

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