Jump to content

understanding this...


alex306

Recommended Posts

Hi there people, i have some coding and I need to understand how it works

 

Thanks people I really need your help

Edited by alex306
Link to comment
Share on other sites

  • Replies 27
  • Created
  • Last Reply

Top Posters In This Topic

  • MSasu

    8

  • alex306

    8

  • BIGAL

    5

  • Dadgad

    4

Top Posters In This Topic

Posted Images

Welcome to the forum.

The only way you are going to be able to explain it to someone else is to do your homework.

On the TUTORIALS tab at the top of this page you can link to a number of LISP TUTORIALS.

Nothing to it.....but to do it.

Better get started. Good luck. :)

Link to comment
Share on other sites

do you think ill fully understand how it works and is it easy to follow? i get confused with all the spaces and the brackets

thanks

Link to comment
Share on other sites

It is very simple to learn how to load this into your system, so that it can be used.

 

If you are hoping to leap into the deep end of the programming pool, without any training, and effortlessly attain lucidity by osmosis?

Wake up and smell the coffee. :)

So which is your mission?

 

Loading it and running it, no sweat. Writing it?

Quite a different can of worms. :wink:

Link to comment
Share on other sites

That code, after loaded like BIGAL instructed you, define a new command named GASKET. This will interrogate about insertion point of a skewed gasket and his half inside dimension [1], respectively wall width [2].

 

32.gif

 

Also need to indicate the number of holes on one side - those seem not to be equal spaced. Last step is to indicate the insertion angle of sketch. The gasket is later drawn skewed at a constant angle. There are no dimensions validations - I mean that some corner(s) may not be rounded, or the holes will overlap.

Link to comment
Share on other sites

I can load the coding into the system fine, and enter the correct values of 300(inside dimentions) 100 (wall thickness) and number of holes on each side 4. but its when i come to the look at the coding and try and understand how its worked out for example I have this....

 

 

 

now in terms of me understanding I'm talking about what setq inpl what that means and LWpolylines Acbd and were all theses numbers come in are they co-ordinates or im not quite sure, and then at the end how come there are steps of closed brackets.. loading the coding i can do by saving the file as _____.lsp then going to autolisp typing in appload find the saved lsp file and then type in gasket and it then i have to type the values it asks. so what I'm trying to do is get an understanding of what each line does in the coding and then how it strings all the information together to perform the action and ask for the values to create the shape. I've got till tomorrow afternoon so ill be going through the tutorials on the site and jotting notes down to help. sorry for sounding like a Autolisp Dummy... I hope with your help and help of tutorials ill master it and be able to explain how it works. because say at a later date, I want to create a totally different shape I would know how to do it, at the moment I have no clue where to start.

 

Thanks guys really appreciate your time and effort

Edited by alex306
Link to comment
Share on other sites

unfortunately Im jumping in the deep :( I want to master it and I'm dedicating my time to it. I'm finding the tutorial helpful, but getting confused with some of the functions, im sure ill get there by tomorrow fingers crossed :)

 

loading and running is correct no sweat. writing it - a lot of head scratching lol :?

Link to comment
Share on other sites

I suggest you to start following an AutoLISP tutorial - the lessons at AfraLISP are a very good option. But I'm affraid that this is not a task to complete in only one day...

While progressing with your learn, try to write your own code and whenever you encounter issues just post that trouble-maker code here and someone will help you to debug it. Good luck!

Link to comment
Share on other sites

I have added comments to your code excerpt:

[color=blue];define a new AutoCAD command, the items from paranthesis are the variables used in code[/color]
[color=blue];(is long because there are many variables defined)[/color]
(defun c:gasket ( / C CI CMDE D ENT INPL K L MPL N OSM OUTPL P1 P12F P14F P2 P21F P23F P3 P32F P34F P4 P41F P43F QAF R1 R2 SS SSCIS SSMPL W) 
(setq osm (getvar 'osmode))  [color=blue] ;retain current value of OSMODE system variable[/color]
(setvar 'osmode 0)            [color=blue];reset said OSMODE to avoid interferences[/color]

[color=blue];add a polyline definition list into drawing's database[/color]
(setq inpl (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") '(90 . 4) '(70 . 1)
                            (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4) '(210 0.0 0.0 1.0)
)
)
)

If I will have time this evening will try to comment the entire routine.

Link to comment
Share on other sites

Thank God time is not going to be an issue. Could there possibly be any topic one couldn't master in 86400 seconds? :?:

I certainly hope not, as I have reserved the OR for early Friday morning when I will be taking a first stab at performing a frontal lobotomy, on myself.

What's a lobe?

 

Like the man said, "I'd rather have a bottle in front of me, than a frontal lobotomy".

 

I admire your faith, and sincerely wish you nothing but the best! :beer:

Link to comment
Share on other sites

As promised, please find below the commented code; I have tried to explain you the meaning of each line; also have made some comments on solutions that others may agree with or not.

You will understand this code better if will follow first some AutoLISP tutorials.

[color=blue];define a new AutoCAD command, the items from parenthesis are the variables used in code
;this is called variable's localization and prevents conflicts with other routines[/color]
(defun c:gasket( / C CI CMDE D ENT INPL K L MPL N OSM OUTPL P1 P12F P14F P2 P21F P23F P3 P32F P34F P4 P41F P43F QAF R1 R2 SS SSCIS SSMPL W)
[color=blue] ;retain current value of OSMODE system variable[/color]
(setq osm (getvar 'osmode))
[color=blue] ;disable all OSNAP modes to avoid interferences when pick or add points
;can be moved below in code since may be useful to input[/color]
(setvar 'osmode 0)
[color=blue] ;retain current value of CMDECHO system variable[/color]
(setq cmde (getvar 'cmdecho))
[color=blue] ;disable command's echoes on prompter (mostly esthetical)[/color]
(setvar 'cmdecho 0)
[color=blue] ;interrogate user to pick a point on screen
;store those coordinates in a variable named "C"[/color]
(setq C (getpoint "\nPick center of gasket : "))
[color=blue] ;ask user to input a distance, store in a variable named "L"
;input as value or dynamically from insertion point[/color]
(setq L (getdist C "\nInner half side length : "))
[color=blue] ;ask user to input a distance, store in a variable named "W"
;input as value or dynamically between two points[/color]
(setq W (getdist "\nConstant wall thickness : "))
[color=blue] ;disable input of O (bit 1) or negative value (bit 2) for next interrogation[/color]
(initget 6)
[color=blue] ;ask user for an integer number, store in a variable named "N"
;repeat the question until certain condition is meet (should be bigger than 3)
;(poorly written since the filter is lost after first wrong input)[/color]
(while (<= (setq N (getint "\nNumber of bolt holes per side [N >= 3] : ")) 2))
[color=blue] ;perform calculation of fillet radiuses based on gasket width[/color]
(setq D (* W 0.4))
(setq R1 (* W 1.2))
(setq R2 (- R1 W))
[color=blue] ;calculate vertexes of gasket's inside edge - the skew angle is hard-coded as 240 degrees
;for calculation the angle must be converted to radians[/color]
(setq p1 (polar (polar C (cvunit 240.0 "degrees" "radians") L) pi L))
(setq p2 (polar p1 0.0 (* 2.0 L)))
(setq p3 (polar p2 (cvunit 60.0 "degrees" "radians") (* 2.0 L)))
(setq p4 (polar p3 pi (* 2.0 L)))
[color=blue] ;add a polyline definition list into drawing's database using DXF codes:
; 0 = entity type, 10 = vertexes, 70 = closed polyline
;lists that require evaluation are built with CONS or LIST function[/color]
(setq inpl (entmakex (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline")
                           '(90 . 4) '(70 . 1)
                           (cons 10 p1) (cons 10 p2) (cons 10 p3) (cons 10 p4)
                           '(210 0.0 0.0 1.0)
                     )
           )
)
[color=blue] ;calculate points around vertexes for fillet operation[/color]
(setq p14f (polar p1 (cvunit 60.0 "degrees" "radians") R2))
(setq p12f (polar p1 0.0 R2))
[color=blue] ;2nd corner[/color]
(setq p21f (polar p2 pi R1))
(setq p23f (polar p2 (cvunit 60.0 "degrees" "radians") R1))
[color=blue] ;3rd corner[/color]
(setq p32f (polar p3 (cvunit 240.0 "degrees" "radians") R2))
(setq p34f (polar p3 pi R2))
[color=blue] ;4th corner[/color]
(setq p43f (polar p4 0.0 R1))
(setq p41f (polar p4 (cvunit 240.0 "degrees" "radians") R1))
[color=blue] ;set the radius for fillet command on each corner and apply it
;(not the best solution since may be affected by either environment or view)
;the underscore ensure that English name of command is used on localized AutoCAD[/color]
(vl-cmdf "_.fillet" "_R" R2)
(vl-cmdf "_.fillet" p14f p12f)
[color=blue] ;2nd corner[/color]
(vl-cmdf "_.fillet" "_R" R1)
(vl-cmdf "_.fillet" p21f p23f)
[color=blue] ;3rd corner[/color]
(vl-cmdf "_.fillet" "_R" R2)
(vl-cmdf "_.fillet" p32f p34f)
[color=blue] ;4th corner[/color]
(vl-cmdf "_.fillet" "_R" R1)
(vl-cmdf "_.fillet" p43f p41f)
[color=blue] ;create middle axis of gasket, store the entity as "mpl"[/color]
(vl-cmdf "_.offset" (/ W 2.0) inpl (polar C pi (+ L (/ W 2.0))) "")
(setq mpl (entlast))
[color=blue] ;create gasket's outside edge, store the entity as "outpl"[/color]
(vl-cmdf "_.offset" W inpl (polar C pi (+ L W)) "")
(setq outpl (entlast))
[color=blue] ;draw a circle in origin, store the entity as "ci"[/color]
(vl-cmdf "_.circle" '(0.0 0.0 0.0) D)
(setq ci (entlast))
[color=blue] ;define a block using above circle, use his center as insertion point[/color]
(vl-cmdf "_.-block" "ci" '(0.0 0.0 0.0) ci "" "")
[color=blue] ;explode middle axis[/color]
(vl-cmdf "_.explode" mpl "")
[color=blue] ;gather items resulted from explosion operation (partial axis)[/color]
(setq ssmpl (ssget "_P"))
[color=blue] ;initiate a counter[/color]
(setq k -1)
[color=blue] ;parse partial axis to insert hole blocks[/color]
(while (setq ent (ssname ssmpl (setq k (1+ k))))
[color=blue]  ;insert differently on lines and arcs and remove the partial axis
 ;PROGN statement is used to block expressions together[/color]
 (if (eq (cdr (assoc 0 (entget ent))) "ARC")
[color=blue]   ;add 1 hole on arc part (divide in 2 parts)[/color]
  (progn (vl-cmdf "_.divide" ent "_B" "ci" "" 2) (entdel ent))
[color=blue]   ;add N - 2 holes on line parts (divide in N - 1 parts)[/color]
  (progn (vl-cmdf "_.divide" ent "_B" "ci" "" (- N 1)) (entdel ent))
 )
)
[color=blue] ;gather all hole blocks from drawing[/color]
(setq sscis (ssget "_X" '((0 . "INSERT") (2 . "ci"))))
[color=blue] ;retain value os QUAFLAGS sysyem variable[/color]
(setq qaf (getvar 'qaflags))
[color=blue] ;allow explosion of multiple items at a time[/color]
(setvar 'qaflags 1)
[color=blue] ;explode all hole blocks[/color]
(vl-cmdf "_.explode" sscis "")
[color=blue] ;restore QUAFLAGS system variable[/color]
(setvar 'qaflags qaf)
[color=blue] ;remove temporary block from drawing to be able to re-use the name on next run[/color]
(vl-cmdf "_.-purge" "_B" "ci" "N")
[color=blue] ;gather items resulted from explosion operation (the holes)[/color]
(setq ss (ssget "_P"))
[color=blue] ;build a selection set from holes, inside and outside edges[/color]
(ssadd inpl ss)
(ssadd outpl ss)
[color=blue] ;ask user to indicate dynamically the insertion angle ("pause" means user input)
;that "_non" is activation None OSNAP mode - superfluous since OSNAP is already disabled[/color]
(vl-cmdf "_.rotate" ss "" "_non" C pause)
[color=blue] ;restore user's environment stored at beginning[/color]
(setvar 'osmode osm)
(setvar 'cmdecho cmde)
[color=blue] ;ensure that routine finish quietly (no nil on command prompt)[/color]
(princ)
)

Link to comment
Share on other sites

Sorry, alex, you're not going to learn AutoLISP overnight. When I first got into it, I already knew a good bit about standard LISP and about AutoCAD, and it was still a challenge. If you're truly serious about learning it, you'll have to roll up your sleeves and experiment. Pick a task and automate it. Also, there are plenty of examples of good code out there to pick apart. Good luck!

Link to comment
Share on other sites

guys your help has been absolutely brilliant, I cant thank you enough, I will follow all the tutorials that I can for as long as I can till get my head round it. Are there any books I could buy? a lot of tutorials tell me to refer to an autocad reference guide or something? i've tried looking for this but I'm unable to find it... and interms of exercises, what would you guys recommend? starting off basic and go into more advanced drawings .... Im starting from here and working my way through, so hopefully putting my glasses on and getting on with it and becoming more knowledgeable. http://www.afralisp.net/autolisp/tutorials/the-basics-part-1.php

 

I really do appreciate the time you guys have taken out to help me understand and thanks for telling me places where to go to get tutorial.

 

In terms of coding can you put together a some of it test what that does then do some more and see what happens (if you understand what i mean) or do you have to do the whole thing and then check it? I feel I'm more a visual learner so that's how I get to grips with any design software.

 

Thanks for everything people :) enjoy your evening and the rest of the week.

Link to comment
Share on other sites

Dont have the Autocad help loaded at moment but Type HELP choose All documentation Customization Lisp it will help explain.

 

Your test ! Read about Getpoint Getreal & Polar then do a draw a rectang as first try hint use Command "Line" pt1 pt2 pt3 "c" rather than entmake a bit simpler for a newbie. In the help there is a page with each command listed alphabetically and they have examples.

 

Like the others I have been writing code for 30yrs and still learning.

Link to comment
Share on other sites

So, you have started your AutoLISP lessons. That a great news!

After you will finish the tutorials at AfraLISP, I suggest to allow some time and parse the help regarding VLEditor, this will help you a lot later when debug your code.

Other learning resources you can find just by doing a search on Forum. The help is also well written and very useful; every time you encounter a new function spend some time to read his help page, understand it and do some tests on VLEditor's console.

 

Regarding the way to write your code - those are called best practices; read this guideline for variables naming (is dedicated for .Net, but the principles are general).

Try to split each routine in small units and ensure that each one is working properly before continue (called unit testing).

 

 

Finally, I propose you to do this exercise after get done the basic tutorials of AfraLISP: Draw the figure below, ask user to indicate the bottom-left insertion point and horizontal, respectively vertical dimension. Post your code here for validation.

 

123dds.gif

Link to comment
Share on other sites

@BIGAL, just noticed that we proposed similar exercises. I will really appreciate if alex306 will post soon his/her solution.

 

It might be better if a moderator will move this thread into AutoLISP section.

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