Jump to content

Recommended Posts

Posted

Hello, 
I have a project to hand in for class but I am a complete beginner in this language. 

I have to select all the circles already drawn on AutoCAD and make a line that represents their diameter (= that cuts them in two). 
We get a bonus point if we can ask the user to choose the width of this line. 

I can draw circles and their diameters but not on circles already drawn. I don't know how to get their data. 

Can you help me? Thank you very much for your help.

 

 

Quote

(defun c:cerc()
  (setq r (getreal "entrer le rayon:"))
  

  (setq pt1 (getpoint "selectionner le centre:"))

  (setq pt2 (polar pt1 0 r))
  (setq pt3 (polar pt2 (/ pi 2) r))
  (setq pt4 (polar pt3 pi r))
  (setq pt5 (polar pt4 (/ (* 3 pi) 2) (* 2 r)))
  
  (command "cercle" pt1 pt2"")
  (command "ligne" pt4 pt5"")     
  
  
)

 

Posted

Here is one way.

 

(setq sel (entsel)) ;select the circle
(setq ent (car sel)) ;grab the entity
(setq exx (entget ent)) ;expand the props from the entity
(setq rad (cdr (assoc 40 exx))) ;radius
(setq cen (cdr (assoc 10 exx))) ;center point
(setq lay (cdr (assoc 8 exx))) ;layer

 

Note there is no error checking, so it presumes that you actually select something, and that is be a circle (no missed picks, no other entity types)

No accommodation for rotated UCS, etc. either..

 

Should help you get started.

 

For example, to draw a line from the left quadrant to the right quadrant, you could do something like this.

 

(setq p1 (polar cen 0.0 rad)) ;find right quad
(setq p2 (polar cen pi rad))  ;find left quad
(entmakex (list (cons 0 "LINE")
                 (cons 10 p1)
                 (cons 11 p2))) ; entmake the line

 

Posted (edited)

Like Rcmcswain a few more hints, using the SSGET function you can select objects and save their entity names, SSGET supports filters, so you can use ssget and a "CIRCLE" filter. once you have a selection set you can loop through them and do something as suggested by Rkmcswain, you can use the REPEAT function. You use ssname to pull an item out of your selection set, note that the 1st item is 0 in the selection not 1.

 

Have a look here ssget Function Reference | Lee Mac Programming (lee-mac.com) a lot of examples. 

 

(setq ss (ssget '((0 . "CIRCLE"))))

(setq ent (ssname ss x))

I notice a language difference you may need to use the English syntax for objects "CIRCLE" "LINE" "ARC" "TEXT"

 

If you get stuck post what you have done.

Edited by BIGAL
  • Like 1
Posted (edited)

Much like BIGAL suggested,  using SSGET can get you to iterate over multiple objects. Once you have an object selected, you can retrieve its properties by using ENTGET on the entity, such as the layer it's on, the center of the circle, the radius, color, etc... You would not then have to specify the center of the circle and the radius manually. You would only then need to prompt the user for the width of the line. 

 

To get a the properties of an entity, you'll need to look at the DXF codes for each of the entities. Upon using ENTGET on an entity, you'll get a list of properties, and you can have a look at their codes from here. Each entity may present a different property even though their DXF codes are the same. For instance, in a circle, code 10 refers to the center of the circle, but for a line, code 10 refers to the starting point. And for instance, in a circle, 40 represents the radius, but for texts, 40 represents text height. You'll just need to learn them through experience and regularly using ENTGET to extract object properties (at least from my point of view). That's why rmkcswain used the codes 10 and 40 to retrieve the center and radius of the circle, and then 10 and 11 for the start and end points of the line respectively.

 

;;; Solution removed... try ahead first ;;;

 

Edited by Jonathan Handojo
Code removed for owner to attempt first.
Posted

Jonathon your to generous "I have a project to hand in for class but I am a complete beginner in this language. 

 

Needed Alexandra1999 to work it out or have a go at least. No good them getting 100/100 and they have no clue how to do it next time 

 

For width of line task maybe make a PLINE using command or entmake pline.

 

 

Posted
2 hours ago, BIGAL said:

Jonathon your to generous "I have a project to hand in for class but I am a complete beginner in this language. 

 

Needed Alexandra1999 to work it out or have a go at least. No good them getting 100/100 and they have no clue how to do it next time 

 

For width of line task maybe make a PLINE using command or entmake pline.

 

 

 

Hmm, you have a valid point BIGAL. I have now removed the code for Alexandra to attempt first.

Posted (edited)

The above responses should get you most of the way there - I would personally approach the problem in the following way:

  • Attempt to obtain a selection set of all circles in the drawing using the ssget function, using the X mode string (to query the entire drawing database) and an appropriate filter list to select entities with entity type (DXF group 0) equal to CIRCLE - examples of this may be found as part of my ssget reference here.
  • Use an if or cond statement to test whether the value returned by the ssget expression is a valid selection set or nil (if no circles could be found), and notify the user if the latter.
  • Prompt the user using the getdist function to specify the width of the line (you could even offer a default option [extra points!] by following my tutorial here)
  • Use an if or cond statement to test whether the user has specified a valid value and either assign a default value or exit the program if the user has pressed Enter/Space/right-click at the prompt.
  • With all input acquired, iterate over the selection set using one of the methods described in my tutorial here.
  • For each circle entity encountered, obtain the DXF data using the entget function.
  • Obtain the circle center (DXF group 10) and radius (DXF group 40) using the assoc function, and use the cdr function to obtain the value associated with these DXF groups.
  • Use the polar function to calculate a point from the center, at a distance equal to the radius, in the desired direction.
  • Use the polar function to calculate a second point either relative to the center (and therefore at a distance equal to the radius) or to the first point (and therefore at a distance equal to twice the radius), in the opposite direction.
  • Construct a 2D Polyline between these two points with constant width equal to that specified by the user - to create the polyline, you can proceed in three different ways:
Edited by Lee Mac
  • Like 1

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