Try the following:;Main Drawing command (defun c:drawfun (setvar 'cmdecho' 0) (setvar 'blipmode' 0) (setq a (getreal' nStartvalue:')) (setq s (getreal' nStep:')) (setq e (getreal' nEndvalue:')) (setq t a) (setq p1 (fnxy t )) (while ( G'day I need to draw a true cosine or sin wave with autocad. Can any one help? A friend said I might try autolisp but I have never used it before. If autolisp is the way to go does anyone know of an online tutorial that can introduce me to using autolisp? I have autocad R14. I don't think you can create a true sinusoid in AutoCAD, but you can create an arbitrarily close approximation:; Draw a one cycle of a sine wave at a given frequency (cycles; per drawing unit) and amplitude (drawing units), starting at (0,0).; Note that the peak-to-peak height of the wave is twice the amplitude.; Uses zero-width polyline straight line segments to approximate the; curve; bulged segments might be better but the math gets messy.; Doing a PEDIT on the result and selecting SPLINE appears to make it; even better.; Requires AutoCAD R11 or higher.
(defun sinewave (freq ampl / count npts elev cycles incr frnp cyinc); Number of polyline vertices to use in the approximation (one more than; this number is actually used) (setq npts 1000; Number of cycles to draw cycles 1; Initialize count of vertices count -1; Increment of angle per point incr (/ (. 2.0 pi) npts); Some constants used in the loop frnp (. freq (float npts)) cyinc (. cycles incr) ) (prompt ' nThinking.' ); Start creating a polyline on the current layer using the; default linetype at the current elevation (entmake (list '(0.
Welcome to Autodesk’s AutoCAD LT Forums. Share your knowledge, ask questions, and explore popular AutoCAD LT topics.
'POLYLINE') '(66. 0.0) (list 10 0.0 0.0 (setq elev (getvar 'ELEVATION'))) ) ); For each point.
(while (= npts (setq count (1+ count))); Create a vertex at the current point (entmake (list '(0. 'VERTEX') '(40. 0.0) (list 10 (. cycles (/ count frnp)) (. ampl (sin (. cyinc count))) elev ) ) ) ); End the polyline (entmake '((0.
'SEQEND'))) (prin1) ) jrf Member of the Autodesk Discussion Forum Moderator Program.
AutoLISP Solutions: Draw a Sinusoidal Curve as a Spline or Polyline 15 Mar, 2005 By: Cadalyst Bypass the process of tracing a curve through many points that must be calculated or constructed This month's request came from Ralph Ragan of Atlanta, Georgia. Ralph wants to draw a sinusoidal curve as a spline or polyline.
This type of curve occurs in many situations, including a two-dimensional helix and screw threads. Normally this is quite time-consuming because it requires tracing a curve through many points that must be calculated or constructed.
The AutoLISP Solution is SINECURVE.LSP, and the corresponding dialog box file SINECURVE.DCL. The routine allows the user to select the periodic time (equivalent to the pitch of a thread), the amplitude (half the height for a thread), the number of cycles to display and the resolution or display accuracy of the curve.
Get the Code Download the files from Cadalyst's CAD Tips site. Save the file in AutoCAD's Support directory. Use the Appload facility by selecting Tools / Load Application, and then use the browser to select the file. How to Use the SINECURVE Code After you load the code, the system prompts you to enter SC to start the program. To see this prompt, you may need to set your Command window size to 3 lines by dragging the Command window splitter bar appropriately. After you enter SC, the Sine Curve dialog box pops up as shown (figure 1).
The Sine Curve dialog box with user-defined selections. You can select the period, amplitude and numbers of cycles as shown in the figure by changing the values in the appropriate edit boxes. The Resolution is controlled by a slider bar as shown.
Sliding the bar to the left (Lo) decreases the resolution and sliding it to the right (Hi) gives the highest accuracy. The resolution is actually the number of points per cycle that is used to define the curve. The lowest setting corresponds to 12 points per cycle, and the highest setting is 144 points per cycle. The initial default value is in the middle of the slider at 72 points per cycle.
In the example shown, the period is 1.0, the amplitude is 0.5 and 2 cycles are selected. The resolution is left at its initial value.
When the OK button is selected the sine curve appears zoomed to its boundaries as shown (figure 2). The curve is a polyline with the FIT curve smoothing option. The sine curve displayed as a polyline. It's possible to convert the polyline to a spline curve, but that would not be as accurate as the fit curve because splines do not pass through their control points. The spline would always result in a curve that is smaller in amplitude than the polyline because the spline curve lies on the concave side of its control points.
Programming Notes The program was written in AutoCAD 2004, and has been tested in both AutoCAD 2004 and 2005, but it should work in any version of AutoCAD later than 2000. SINECURVE.LSP starts as usual with my error handler and system variable functions. The function SINE is the dialog box driver function that calls INIT-SINE to initialize the settings in the dialog box. If the program has already been run in the current session of AutoCAD, then all the dialog box data values are reused as new defaults because the period, amplitude, number of cycles and resolution are stored in global variables. Immediately following INIT-SINE, the DRAW-SINE-IMAGE function is called to create the image of a sine curve. I favor this method of creating images over using slides because there are fewer files to keep track of.
Even if the LISP program is compiled, it is not possible to include slide libraries in the compiled set of data. I have previously written functions to draw lines (vectors), arcs and circles in image tiles for dialog boxes, and in this case only the vector is required.
The code to draw the sine curve takes a number of definition points NP, and runs a repeat loop as follows: (repeat (fix np) (setq y (- y0 (. A (sin (/ (. 2 pi (setq x (+ x xinc))) xmax)))) p2 (list x y) ); end of setq (startimage 'sine') (do-vector p1 p2 col) (endimage) (setq p1 p2) ); end of repeat For this function, I set the number of points NP at 144, which gives a very smooth-looking image, as shown in figure 1. The code is written using the points p1 and p2, much the way that would be used to program lines between two points on the regular AutoCAD screen. This is possible because the function DO-VECTOR converts the points to the x- and y-integer values required by the AutoLISP VECTORIMAGE function. A small inconvenience in drawing image tiles is caused by the reversed positive direction of the y-axis.
The x- and y-origin of image tiles is at the top left instead of at the lower left. This has always struck me as a particularly silly decision that was clearly made by a graphically challenged mathematician with a matrix fetish. Compare the above code to that of the main Sinecurve function that collects the points necessary to draw the polyline in the following fragment: (repeat (. R n) (setq y (. A (sin (/ (.
2 pi (setq x (+ x xinc))) P))) p2 (list (+ x x0) (+ y y0)) ); end of setq (setq plist (append plist p2)) ); end of repeat (setq polyobj (make-LWpolyline plist)); polyline object (vl-cmdf 'Pedit' 'L' 'Fit' ') Here there are no negative signs to turn the world upside down. After this repeat loop the function Make-LWpolyline is called to create the polyline, which is then converted to a FIT curve by the last line of code as shown. The (setq polyobj is not strictly necessary here, but I included it to ensure that the appropriate object was returned during coding and testing.
The function Make-LWpolyline is shown here to illustrate the object-oriented approach to making a polyline in Visual LISP: (defun make-LWpolyline (plst) (setq arr (vlax-make-safearray 5 (cons 0 (- (length plst) 1)))) (vlax-safearray-fill arr plst) (setq pline (vla-AddLightweightPolyline.modelspace. (vlax-make-variant arr) ); end of vla-AddLightweightPolyline ); end of setq ); end of make-LWpolyline As always, I look forward to receiving your requests for AutoLISP Solutions. Contact me using the links below.