Function to sum dx with dy

Function to sum dx with dy

jackson.herringQ95E7
Explorer Explorer
973 Views
17 Replies
Message 1 of 18

Function to sum dx with dy

jackson.herringQ95E7
Explorer
Explorer

Hello,

 

I am trying to automate a small part of my job by creating a macro/function that will take the output of the DIST command on a point to point measurement, sum dx and dy, and multiply them by a factor, then output the final number.

 

Does anyone know of where to even start with this? I have never even so much as recorded a macro in AutoCAD, but I would love to use my calculator a few hundred times a day less than I do now.

0 Likes
Accepted solutions (2)
974 Views
17 Replies
Replies (17)
Message 2 of 18

john.uhden
Mentor
Mentor

@jackson.herringQ95E7 

The DIST command returns only the distance between two points.

How about if we create you a new command that has you pick two points and then returns the solution you desire?

What do you want to name the new command, maybe DXDY?

John F. Uhden

0 Likes
Message 3 of 18

rgrainer
Collaborator
Collaborator

Here's one place to start:

1 Mathematics

+ --------- Add
- --------- Subtract
* --------- Multiply
/ --------- Divide
rem ------- Remainder of integer division
1+ -------- Increment by one
1- -------- Decrement by one
abs ------- Absolute
fix ------- Truncates a real to an integer
float ----- Converts an integer to a real
gcd ------- Greatest common denominator
min ------- Smallest (least) of group
max ------- Largest (greatest) of group
sqrt ------ Square root
expt ------ Exponent
exp ------- Power of e
log ------- Natural log
cvunit ---- Converts a value from one unit to another

2 Geometry & trigonometry

distance -- Returns distance between two points
angle ----- Returns angle between two points
polar ----- Returns a point at a given distance and angle from a base point
inters ---- Returns point at which two lines intersect
sin ------- Sine
cos ------- Cosine
atan ------ Arctangent

3 Setting variables

setq ------ Assigns a value to a variable (variable automatically quoted)
set ------- Assigns a value to a variable (must quote the variable)
also see setvar under "14 - Working with AutoCAD"

4 User input

initget --- Controls input allowed in next user input function
getint ---- Allows user to enter an integer
getreal --- Allows user to enter a real
getpoint -- Allows user to pick a point (rubber-band is a line)
osnap ----- Returns a point snapped to some feature of existing geometry
getcorner - Allows user to pick a point (rubber-band is a rectangle)
getdist --- Allows user to pick a distance

Then do a search here for getdist and see what others have done.
Then make an effort to cobble something minimal together and people here will gladly help you with any questions you might have. You'll be creating lots of small functions to help ease other tasks before you know it.
Also, take a look on youtube for how to use the Autocad macro recorder. It may be all you need. 

0 Likes
Message 4 of 18

Sea-Haven
Mentor
Mentor

Like John some lisp hints, it would be better to learn about lisp rather than the Macro editor it will open a big world for you.

A good real beginner task. Check out lisp help for commands. 

AutoLISP Functions | AfraLISP

 

(getpoint do this twice p1 p2

(distance p1 p2)

(angle p1 p2)

(car p1) dx1

(cadr p1) dy1

(distance dx1 dx2) distance between the 2 X values say save as Dx

do dy1 dy2

(princ (* (+ Dx Dy) factor)) answer is displayed.

 

You can use Notepad to code in or the better is "Notepad++" its a free editor and has lisp checking built in. Can code and test using the attached lisp to auto load your code from Notepad++.

 

0 Likes
Message 5 of 18

jackson.herringQ95E7
Explorer
Explorer

Yes this is exactly what I am trying to do, I will explore some of the LISP suggestions in this thread tomorrow and hopefully be back with some more specific questions, thank you!

0 Likes
Message 6 of 18

Sea-Haven
Mentor
Mentor

Get stuck just post again, but you will get lots of help by having a go rather than please provide.

0 Likes
Message 7 of 18

CodeDing
Advisor
Advisor

@jackson.herringQ95E7 ,

 

If you're going to use it in a macro, you might want to consider looking into the CAL command. This is a command as well as a transparent command (can be used inside of a running command). It allows for user selection of points and you can specify your scale factor.

 

For example, run the CAL command, then put this in as the expression. It will allow you to return 2x the distance between 2 user-selected points.

2*abs(vec(cur,cur))
Command: CAL
>> Expression: 2*abs(vec(cur,cur))
>> Enter a point:
>> Enter a point:
200.02

 

Best,

~DD

0 Likes
Message 8 of 18

Kent1Cooper
Consultant
Consultant

@rgrainer wrote:

....

rem ------- Remainder of integer division

...


A small correction:  (rem) gives the remainder left over from any division, not limited to integers either in arguments or result:

 

Command: (rem 12.345 2.345)
0.62

Kent Cooper, AIA
0 Likes
Message 9 of 18

jackson.herringQ95E7
Explorer
Explorer

So far this is what I've got in autoLISP:

 

(getpoint) p1

(getpoint) p2

(distance p1 p2)

(angle p1 p2)

(car p1) dx1

(cadr p1) dy1

(car p2) dx2

(cadr p2) dy2

(distance dx1 dx2) Dx

(distance dy1 dy1) Dy

(princ (* (+ Dx Dy) 1.100))

 

This loads and lets me pick the two points, then returns:

 

error: bad argument type: 2D/3D point: nil

 

So at least there is a tiny amount of progress, however I am starting from absolute zero and don't know anything about coding in general, let alone the syntax of autoLISP specifically.  Any help would be greatly appreciated, thank you!

 

 

0 Likes
Message 10 of 18

Kent1Cooper
Consultant
Consultant
Accepted solution

@jackson.herringQ95E7 wrote:

So far this is what I've got in autoLISP:

(getpoint) p1

....

This loads and lets me pick the two points, then returns:

error: bad argument type: 2D/3D point: nil

....


I think the format in Message 4 was misleading.  You need something more like [untested]:

(setq

  p1 (getpoint)

  p2 (getpoint)

  ;;;; (distance p1 p2) ;;;; [ not needed ]

  ;;;; (angle p1 p2) ;;;; [ not needed ]

  dx1 (car p1)

  dy1 (cadr p1)

  dx2 (car p2)

  dy2 (cadr p2)

  Dx (abs (- dx1 dx2)) ;;;; (distance) functions inappropriate with just numbers [not points]

  Dy (abs (- dy1 dy1))

); end setq

(princ (* (+ Dx Dy) 1.1))

 

[The two zeroes on the end of 1.100 served no purpose.]

Kent Cooper, AIA
0 Likes
Message 11 of 18

CADaSchtroumpf
Advisor
Advisor
Accepted solution

Try to understand this, is a good start for learning lisp with good practice.

 

(defun c:DxDy ( / p1 p2 factor)
	(if (zerop (getvar "USERR1"))
		(setvar "USERR1" 1.0)
	)
	(initget 8)
	(while (setq p1 (getpoint "\nGive the first point : "))
		(initget 41)
		(setq p2 (getpoint p1 "\nGive the second point : "))
		(initget 6)
		(setq factor (getreal (strcat "\nFactor to apply? <" (rtos (getvar "USERR1")) ">")))
		(if (not factor)
			(setq factor (getvar "USERR1"))
			(setvar "USERR1" factor)
		)
		(princ
			(strcat
				"\nResult of ("
				(rtos (abs (- (car p2) (car p1))))
				" + "
				(rtos (abs (- (cadr p2) (cadr p1))))
				") X "
				(rtos factor)
				" = "
			)
		)
		(princ (* (+ (abs (- (car p2) (car p1))) (abs (- (cadr p2) (cadr p1)))) factor))
		(initget 8)
	)
	(prin1)
)

 

0 Likes
Message 12 of 18

jackson.herringQ95E7
Explorer
Explorer

Yeah thank you, after making my last post I learned about setq. I actually had gotten to the point where I was getting an error from using DISTANCE rather than just doing the arithmetic. Also I had no idea you could just nest everything in one setq, that is certainly a lot more elegant than what I had.

 

Thanks again for the help, this is exactly what I needed, and I think I learned enough that I can tweak it in the future!

 

Message 13 of 18

john.uhden
Mentor
Mentor

@CADaSchtroumpf 

How about...

(* factor (apply '+ (mapcar 'abs (mapcar '- p2 p1 '(0 0)))))

?

John F. Uhden

Message 14 of 18

CADaSchtroumpf
Advisor
Advisor

@john.uhden 

Yes, is more elegant!

0 Likes
Message 15 of 18

CADaSchtroumpf
Advisor
Advisor

But I think what use 'apply and 'mapcar is hard for a beginner

0 Likes
Message 16 of 18

john.uhden
Mentor
Mentor

@CADaSchtroumpf 

Yes, apply and mapcar were like secret code to me for a long while.  I still require plenty of trial and error to get them right.  It was just in the last month that @hak_vz or maybe @ronjonp confirmed that mapcaring a shorter list with a longer list will return the length of the shorter (or shortest).  That's how I avoided the Z value.

But that's the purpose of this place... to make others aware of and hungry for special tools, especially the ones they learn to make on their own.

John F. Uhden

0 Likes
Message 17 of 18

CADaSchtroumpf
Advisor
Advisor

Yes, it can be part of learning....

0 Likes
Message 18 of 18

Sea-Haven
Mentor
Mentor

How about

(* factor (apply '+ (mapcar 'abs (mapcar '- (getpoint "\nPick point ") (getpoint "\nPick second point") '(0 0)))))

 

(* (getreal "\nEnter factor value") (apply '+ (mapcar 'abs (mapcar '- (getpoint "\nPick point ") (getpoint "\nPick second point") '(0 0)))))
(* (if (= factor nil)(setq factor (getreal "\nEnter factor value")) (eval factor)) (apply '+ (mapcar 'abs (mapcar '- (getpoint "\nPick point ") (getpoint "\nPick second point") '(0 0)))))

 

0 Likes