Jump to content

to get center of the to the two circle and length-width of rectangle by single click.


magan

Recommended Posts

Hello everyone.

I am currently using AutoCAD2014.

I am trying to code the program using AutoLISP.

 

I am having 3D solid with some holes.

I want to get the center of the circular hole and (length-width) of rectangular hole. and the output data has to be printed in the command prompt of autoCAD as output.

This is all to be done with just single click on object.

I am here providing the image of my object as well.

Please, someone help me.

s2.jpg

Link to comment
Share on other sites

  • Replies 26
  • Created
  • Last Reply

Top Posters In This Topic

  • hanhphuc

    10

  • magan

    7

  • sonali

    5

  • amili

    2

Top Posters In This Topic

Posted Images

Maybe copy object to somewhere explode twice you can find circles but rectang becomes lines a bit of smart coding would reveal LxW use bounding box of 3d solid for limits and copy a known amount so you can work out object cen pts etc. use (ssget (assoc 0 "Circle,Lines"))

 

Lee-mac posted a possible solution for this http://www.cadtutor.net/forum/showthread.php?35506-How-to-get-Region-coordinates/page2

Link to comment
Share on other sites

Thank you for the help, But,I don't know what is the reason it's not working. Let me make this problem easy.I will initially work in the 2D of this component. If this 2D component is blocked as one.

And then if I want centres of circles and (length-width) of rectangle, what can be done?

 

Please help me.

s4.jpg

Link to comment
Share on other sites

sir, can you give me much program hint regarding this. Because, I am new in this coding, which is making difficulty in proceeding further. please help me sir..

Link to comment
Share on other sites

This example is just start point

which to get circle coordinates from solid.

method as proposed by Mr.Alan (BIGAL) at post#2

 

update v1.1

additional export csv as requested by amili @post#15

7/02/15

(vl-load-com)
(defun c:test (/ i e p1 p2 ss lst q var[color="red"] f fn dat dat1[/color]) 
;hanhphuc 2014
 (set 'var (getvar 'cmdecho ))
 (setvar 'cmdecho 0) 
 (if (and (setq e (entsel "\nPlease select solid.. ")) (setq e (car e)) (= (cdr (assoc 0 (entget e))) "3DSOLID"))
   (progn (vla-GetBoundingBox (setq obj (vlax-ename->vla-object e)) 'p1 'p2)
   (mapcar ''((a b) (set a (vlax-safearray->list b))) '(p1 p2) (list p1 p2))
   (command "_explode" e)
   (setq i   0
	 ss  (ssget "C" p1 p2)
	 lst (mapcar '(lambda(x)
			(setq q nil)
			(if
			 (= (cdr (assoc 0 (entget x))) "REGION")
			 (setq q (cons (LM:reg x) q))
			 (setq q (cons (vlax-ename->vla-object x) q))
			 )
			(if
			 (listp q)
			 (LM:flatten q)
			 q
			 )
			)
		     (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
		     ) ;_ end of mapcar
	 ) ;_ end of setq
     
   (foreach o (vl-remove-if-not ''((x) (= (vla-get-ObjectName x) "AcDbCircle")) (LM:flatten lst))
     
     (setq dat(cons (princ (strcat [color="red"]"\nCIRCLE_"[/color] (itoa (setq i (1+ i))) [color="red"]" "[/color]
	     (vl-princ-to-string
		 (mapcar ''((x)(vlax-get o x)) '(Radius Center))
	       )))
		     dat))
       
     ) ;_ end of foreach

   (command "_.U")


[color="red"](setq fn (strcat (getvar "dwgprefix") "hole dat.csv") f (open fn "w"))
[color="blue"] ; If you don't want to override file ,to append use (open fn "a") as suggested by Marko @ post#14[/color]

(foreach $
(foreach x dat
 (setq	dat1 (cons (vl-string-translate
	     " "
	     ","
	     (vl-list->string
	       (vl-remove-if ''((a) (or (= a 10) (= a 40) (= a 41))) (vl-string->list x))
	       ) ;_ end of vl-list->string
	     ) ;_ end of vl-string-translate
	   dat1
	   ) ;_ end of cons
) ;_ end of setq
 ) ;_ end of foreach
(write-line $ f))
(write-line " " f)
(if f (close f))
(startapp "notepad" fn)[/color]     [color="blue"];<--remove this line if you don't want it to pop-up everytime[/color]
   ) ;_ end of progn
   ) ;_ end of if
 (setvar 'cmdecho var)
 (princ)
 ) ;_ end of defun

 

sub-functions credit to Lee Mac

;;;http://www.cadtutor.net/forum/showthread.php?35506-How-to-get-Region-coordinates/page2
;;;adopted as sub-function
(defun LM:reg (reg / RetObj)
 (setq Reg (vlax-ename->vla-object reg))
 (if (vlax-method-applicable-p reg 'explode)
 (progn
 (setq RetObj (vlax-safearray->list (vlax-variant-value (vla-explode Reg))))
 (repeat (length RetObj)
   (if	(eq "AcDbRegion" (vla-get-ObjectName (car RetObj)))
     (setq RetObj (append RetObj (vlax-safearray->list (vlax-variant-value (vla-explode (car RetObj))))))
     (setq RetObj (append RetObj (list (car RetObj))))
     ) ;_ end of if
   (setq RetObj (cdr RetObj))
   ) ;_ end of repeat
 )
   )
 retobj
 ) ;_ end of defun


;; Flatten List  -  Lee Mac
;; Transforms a nested list into a non-nested list
;; http://www.lee-mac.com/flatten.html

(defun LM:flatten ( l )
   (if (atom l)
       (list l)
       (append (LM:flatten (car l)) (if (cdr l) (LM:flatten (cdr l))))
   )
)

 

*The overlapped circles can be filtered using x,y coordinates (2D)

Edited by hanhphuc
export csv
Link to comment
Share on other sites

Mr. hanhphuc, it is showing error as "error: no function definition: VLAX-ENAME->VLA-OBJECT". and one more thing that I want to ask you that, can you give me the complete single program , by which i can exactly the centres of both the circles.?

Link to comment
Share on other sites

add this on top

[color="red"](vl-load-com)[/color][color="gray"]
(defun c:test (/ i e p1 p2 ss lst q) 
...
...
...[/color]

Due to its 3D solid there will be 2 circles (upper & lower) for each position, difference in z value

ie: 2 x 2 = 4 nos circle

Link to comment
Share on other sites

Thank you so much,,,, for your help. Its actually working. Sir, is it possible to get the radius of these circles too.?

glad to help, credit to BIGAL's idea & also Lee Mac's sub-functions.

 

yes, radius can be added

updated in post #6

 

example output, radius is in red:

CIRCLE 1: ([color="red"]1.19302[/color] (-38.7769 5.2797 2.65292))
CIRCLE 2: ([color="red"]1.19302[/color] (-38.7769 5.2797 0.0))

Link to comment
Share on other sites

Thank you so so so much,,sir.......................

It's unbelievable. I was trying for it from so many days. Thanks lot.

 

But, when it is opened in some computer's other drawing, it just explodes whole geometry, and says: bad argument,

also second problem i am facing is that sometimes it is repeating the result and counting that as circle. For example, for the 3 hole in solid, is shows 8 circles. Please help me sir, in this regard.

Link to comment
Share on other sites

Thank you so so so much,,sir.......................

It's unbelievable. I was trying for it from so many days. Thanks lot.

- you're welcome -

But, when it is opened in some computer's other drawing, it just explodes whole geometry, and says: bad argument,

also second problem i am facing is that sometimes it is repeating the result and counting that as circle. For example, for the 3 hole in solid, is shows 8 circles. Please help me sir, in this regard.

Disadvantage of this single click method

1. ssget crossing may collect others item.

2. exploded solid contains many objects, may be it looks like a circle, it be could an ellipse?

3. we can't assure the the circle is correct hole position.

 

im not sure repeating result. bad argument etc..

you can attach drawing. save as 2007 more generic

 

p/s: if the hole is not tapper, can try bpoly method click on hole(s) , then ssget.

It would be more accurate also can collect the rectangular center too. my $0.02 :)

Link to comment
Share on other sites

Oh,,, sure sir.

Your help is invaluable.

I will always be grateful to you as a student.

 

Sir, if you don't mind, I want to ask you some more question which is making it difficult to use output.

Sir, I am trying to get the radius and center on consecutive line.

And one more thing that I want to do is, I want to save whole data in a single or separate data file (.dat). But, because our code is working on loop, the data saved in the .dat file is get over writed in the same .dat file. Means it saves only last result, not all.

 

I am right now using this for saving the content in .dat file :

 

(setq fo (open "D:/n_hole.dat" "w"))

(princ mtw fo)

(close fo)

(princ)

 

My ultimate gole of doing this is to get the number of hole, minimum diameter of hole and finally distance between all holes. Help me, if possible.

Link to comment
Share on other sites

  • 2 months later...

Hey...this code is useful to me too... Thanx hanhphuc for answer and Thanx magan for this question.

The problem I am facing with this code is, the data that I am saving in excel file, are not properly saved. Also, the coordinates (x y z) of centers of the circle, we get by this code are saved as single number, that just simple space between these coordinates (ex. (20 10 10)). So, I want that coordinates of center should be saved in different cells. Please help me... I need it.

Link to comment
Share on other sites

Hey...this code is useful to me too... Thanx hanhphuc for answer and Thanx magan for this question.

The problem I am facing with this code is, the data that I am saving in excel file, are not properly saved. Also, the coordinates (x y z) of centers of the circle, we get by this code are saved as single number, that just simple space between these coordinates (ex. (20 10 10)). So, I want that coordinates of center should be saved in different cells. Please help me... I need it.

hi amili welcome to CADTutor :)

my understanding you want it to be separated for each cell?

for convenient, save as csv format

circle_# , Radius , X , Y , Z

 

Updated in post #6

Link to comment
Share on other sites

Thank u so much sir,,,, It is so special for me that I cannot explain it to you. Again thank you sir. If you dont mind I want to ask you some more question regading my work.

 

As what have been done with the circular holes, can it be done with the rectangular holes?

IF yes then I will have to get the following data from that rectangle:

1. Center of the rectangle

2. Points of the rectangle

3. And Length , width of the rectangle.

 

For your refernce, I am attaching screenshot of my drawing as well as expected output excel file. It will be very much fortunate for me if these can be done with Autolisp.

Please help me.

rect 1.jpg

excel sample.jpg

Link to comment
Share on other sites

  • 4 weeks later...

Hey Amili....

Your problem is solved, thanx to hanhphuc and lee-mac. In the post # 6, replace the AcDbCircle with AcDbPolyline.

 

But, hanhphuc , I want to ask you one thing that, the data file which is generated continuously saves previous data, though the file is deleted. So, some previous garbage values are collected in the data fie. I want to remove those unnecessary values.

 

Can you help in this regard??

Link to comment
Share on other sites

....the data file which is generated continuously saves previous data, though the file is deleted. So, some previous garbage values are collected in the data fie. I want to remove those unnecessary values.

 

Can you help in this regard??

 

sorry for the late reply..

with

(open fn "w") ;this should override the previous data.

unless

(open fn "a") ;this keeps appending data

 

i'm not sure does Windows restrict it? (some pc can't even create file in directories by lisp.)

Link to comment
Share on other sites

Thank you sir, for your reply.

 

Yes sir, I have tried both "w" and "a". But, it's not working. In your program, it is not repeating the data, if we go for getting the data for circle only. But, the problem comes when, I tried to get both the polylines and circles, in two different csv files respectively. So, please help me something about it.

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