TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: noadea on April 16, 2007, 05:49:21 AM
-
Hi All,
I have a problem to manipulate the data list. Here are example of data in csv format:
1 1
2 -
3 -
4 -
5 4
6 5
7 6
8 -
9 7
10 8
11 -
12 -
13 10
From the data list, i suppose to draw line according to data by row. For better understanding, I attached the roughly drawing to describe my need. If refer to data square1 connect to circle1, square5 to circle4, square6 to circle5... But the problem here is the circles with "-" value in csv file. Example square2, square3 and square4 which connect to 3 different points between circle1 and circle4. How to get the different points refer to the total of "-" at the time? (3 points between circle1 and circle4, 1 point between circle6 and circle7, 2 points between circle8 and circle10). Then the line should be drawn from square to point according to data in row. Thanks.
-
Got any code so we can see what you have so far?
-
Here I attached the code that I have for this moment.
-
For thoses interestd, here is the original thread:
http://forums.augi.com/showthread.php?t=58847
-
This problem is outside of my programming level. I hope there are experts would help me solve this problem. Thank you.
-
Where is the datalist for a sampling of squares and circle locations ?
-
Is the connection pattern different each time ?
Do you have a sample csv file ?
Do you have a sample drawing file. ?
at least give us ALL the information.
How many of these are you doing. ?
Is it a school project ?
-
Here are sample of drawing and csv file.
-
Good questions Kerry, I hope we get the answers.
I had very little time yesterday & less time today. :(
But here is what I started with. An assumption about the data list from the cvs file.
Then several hoops to get the data in a usable form.
I was about to replace the nil values according to the specifications when time ran out.
So here is what I have so far. Maybe tomorrow will be better for me. :?
(defun c:test ()
;; given output from CVS file
(setq output '("5,23,27,24" "5,22,-,-" "5,21,-,-"
"5,20,-,-" "5,19,29,19.5" "5,18,30,17.75"
"5,17,30,16.5" "5,16,-,-" "5,15,30,15"
"5,14,30,13" "5,13,-,-" "5,12,-,-"
"5,11,30,10"
)
)
;;===========================
;; parser by CAB single character delim
(defun sparser (str delim / ptr lst)
(while (setq ptr (vl-string-search delim str))
(setq lst (cons (substr str 1 ptr) lst))
(setq str (substr str (+ ptr 2)))
)
(reverse (cons str lst))
)
;; CAB 05/07/06
;; group on the first two elements A B of a flat list A B C D
;; InpLst is the flat list ((A B) (C D) (E F)...)
(defun group_on2 (InpLst / OutLst tmp grp idx sub)
(while InpLst
(setq OutLst (cons (list (car InpLst) (cadr InpLst)) OutLst))
(setq InpLst (cddr InpLst))
)
(reverse OutLst)
)
;; create a list of list with 4 items in each sub list
(setq lst (apply 'append (mapcar '(lambda (x) (sparser x ",")) output)))
;; convert to numbers
(setq lst (mapcar '(lambda (x)(if (numberp (read x))(read x))) lst))
;; group in pairs
(setq lst (group_on2 (group_on2 lst)))
;; Replace nil points
;| -=< Under Construction >=-
(setq idx 0)
(while (< idx (length lst))
(cond
((null stpt) (setq stpt (nth idx lst)))
((null (cadr (nth idx lst)))
)
)
)
|;
(princ)
)
-
Oops, see there, I guessed wrong on the data. :-( The groups of 4 are reversed & there is an index number.
From CVS file:
("56,-,-0.62766,0.19281"
"55,-,-0.71186,0.19281" "54,-,-0.33241,0.69237"
"53,-,-0.41661,0.69237" "52,-,-0.50081,0.69237"
"51,-,-0.83871,0.69237" "50,-,-0.92291,0.69237"
......................
)
Looks like this "56,-,-0.62766,0.19281" needs to be decoded to
this (0.19281 -0.62766) (nil nil) which are two points.
Gotta go..
-
FYI
Regarding to the data in csv file:
column A : number according to square
column B : number according to circle
column C : x-coordinate of square's centre point
column D : y-coordinate of square's centre point
-
This does not match the drawing.
From the CVS file:
1 1 -1.4668 1.1332
From the Drawing, Pad 1
BLOCK REFERENCE Layer: "Pad"
Space: Model space
Color: 6 (magenta) Linetype: "Continuous"
Handle = 326F
"GDM7211_VCO_TEST.Pad"
at point, X=0'-8 9/16" Y=0'-1 19/32" Z= 0'-0"
X scale factor 1.00000
Y scale factor 1.00000
rotation angle 0.0000
Z scale factor 1.00000
Note that x = 8.5625 y = 1.59375
Why the discrepancy?
-
Alan, those coordinates are relative to
center of detail (to the crosshair)
-
Oh, thanks Fatty.
I never would have guessed. :?
So that means we are only interested in column A & B.
In the original post I thought the data file contained points for A & B but as we see now they contain text labels.
-
Oh, thanks Fatty.
I never would have guessed. :?
So that means we are only interested in column A & B.
In the original post I thought the data file contained points for A & B but as we see now they contain text labels.
Alan,
I've tried to solve ir entire last weekend
with no luck
Perhaps my poor brain is not in the good
condition at the moment :-)
~'J'~
-
OK, I think I understand the goal. That is often 90 percent of the battle.
I am out of time again today, but this is where I ended up.
Still needs work AND I am not sure the CVS data is correct.
Why is 5 connected to 4? ("5" "4" "-1.4668" "0.3832")
And 14 connected to 11? ("14" "11" "-0.9832" "-1.4668")
<edit: usless code removed>
-
The idea is there already. The csv file is correct.
If you run my code on that drawing, you will get like in tst1.dwg.
But I want it be like tst2.dwg.
-
Ah, the mystery unfolds. 8-)
-
Ah, the mystery unfolds. 8-)
Yeah, I gave up cause getting information was worse than pulling teeth.
-
This is the new attempt.
But still got wrong somewhere cause there are several points which are wrong connection then also not connected. Please help correct it. Thanks.
-
Needs error checking, but worked in my test.
(defun strlist (strexp strdel / itm )
(setq strlst nil)
(while (setq pos (vl-string-position (ascii strdel) strexp))
(setq itm (substr strexp 1 pos))
(setq strlst (append strlst (list itm)))
(setq strexp (substr strexp (+ pos 2)))
)
(setq strlst (append strlst (list strexp)))
)
;(defun read-csv ( / rdlst output)
(defun read-csv ()
(setq output nil
rdlst nil)
(setq fname (getfiled "Select a .csv file :" (getvar "dwgprefix") "csv" 16))
(setq fname (open fname "r"))
(while
(setq rdlst (read-line fname))
(setq output (cons rdlst output))
):end while
(close fname)
(mapcar (function (lambda (x)(strlist x ",")))
(reverse output)
)
)
(defun c:oln1a()
(setq OldOsmode (getvar "OSMODE"))
(setvar "OSMODE" 0)
(setq adoc (vla-get-activedocument (vlax-get-acad-object))acsp (vla-get-block (vla-get-activelayout adoc)))
(setq data (read-csv))
(setq CutObject (vlax-ename->vla-object (car (entsel "\nSelect Ground Bond Line...."))))
(setq CVSdata ;= list csv (including gl's)
(mapcar (function (lambda(x)
(list (car x)
(cadr x)
)))
data));end setq CVSdata
;get all squares
(setq Squaretxtlst nil)
(setq st (ssget "_X" (list (cons 0 "INSERT")(cons 8 "Pad"))))
(vlax-for a (vla-get-activeselectionset adoc)
(vla-GetBoundingBox a 'MinPnt 'MaxPnt)
(setq SquareInsPnt (vlax-get a 'InsertionPoint))
(setq MinPt (vlax-safeArray->list minPnt))
(setq MaxPt (vlax-safeArray->list maxPnt))
(vl-cmdf "._zoom" "_E") ; needed for ssget _W, will not work if not visiable on screen
(setq SquareTxt (cdr (assoc 1 (entget (ssname (ssget "_W" minPt maxPt '((0 . "TEXT")))0)))))
(setq SquareTxtLst (append SquareTxtLst (list (cons (atoi SquareTxt) SquareInsPnt ))))
);end vlax-for
(setq CircleTxtLst nil)
(setq sc (ssget "_X" (list (cons 0 "CIRCLE")(cons 8 "Tag"))))
(vlax-for a (vla-get-activeselectionset adoc)
(setq ip (vlax-get a 'Center))
(setq rad (vlax-get a 'Radius))
(setq p1 (list (- (car ip)(* rad 3))(- (cadr ip)(* rad 3)))
p2 (list (+ (car ip)(* rad 3))(+ (cadr ip)(* rad 3)))
cp (list (car ip)(cadr ip) 0.0)
)
(setq txtstr (cdr (assoc 1 (entget (ssname (ssget "_W" p1 p2 '((0 . "TEXT"))) 0)))))
(setq CircleTxtLst (append CircleTxtLst (list (cons (atoi txtstr) ip))))
);end vlax for
(setq count 0) ; nth pointer
(setq datalen (1- (length CVSdata)))
(while (< count datalen)
(if (not (vl-position (cadr(nth count CVSdata)) '("GL" "-")))
;; Add a Wire
(progn ; no GL found
(setq SqPointer (atoi (car(nth count CVSdata))))
(setq CirPointer (atoi (cadr(nth count CVSdata))))
(setq lnobj (vlax-invoke acsp 'AddLine (cdr (assoc SqPointer Squaretxtlst))(cdr (assoc CirPointer circletxtlst))))
(vlax-put lnobj 'Layer "Wire")
(setq count (1+ count))
);end progn no GL found
;; Else Add Ground Wire
(progn ; GL found
(setq SqPointer (atoi (car(nth count CVSdata))))
(if CirPointer
(setq CirPnt1 CirPointer) ;CirPointer = last known used circle
(setq CirPnt1 1)
)
;; find next valid Wire
(setq CirPnt2 (1+ count))
(while (vl-position (cadr(nth CirPnt2 CVSdata)) '("GL" "-"))
(setq CirPnt2 (1+ CirPnt2))
)
(if (< CirPnt2 (length CVSdata))
(progn
(setq CirPnt2 (atoi (cadr (nth CirPnt2 circdata))))
(setq pt1 (cdr (assoc CirPnt1 circletxtlst))
pt2 (cdr (assoc CirPnt2 circletxtlst)))
(setq ang (angle pt1 pt2))
(setq stepcnt (- CirPnt2 CirPnt1))
(setq stepdis (/ (distance pt1 pt2) (+ stepcnt 1)))
;; Create the Ground Wire(s)
(setq idx 1)
(repeat stepcnt
(setq Cutlnobj (vlax-invoke acsp 'AddLine (cdr (assoc SqPointer Squaretxtlst))
(polar pt1 ang (* stepdis idx))
)
)
(setq InterSect (vlax-invoke CutLnObj "intersectwith" CutObject acextendnone))
(vla-delete Cutlnobj)
(setq lnobj (vlax-invoke acsp 'AddLine InterSect (cdr (assoc SqPointer Squaretxtlst))))
(vlax-put lnobj 'Layer "Wire GB")
(setq idx (+ idx 1))
(setq SqPointer (+ SqPointer 1))
)
(setq count (+ count (1- idx)))
)
(setq count (length CVSdata))
)
);end progn GL found
);end and
);end while
(setvar "OSMODE" OldOsmode)
);end defun
-
noadea,
I see you were here last night. No comment though.
Did the routine work for you as you desired?
-
Hi CAB,
It works fine on that csv file. But it doesn't work on the csv file that has 'GL' value in the first line of the data. For example if using this attached csv file. Anyway thanks a lot for your help.
-
OK, I fixed that, but another problem cropped up.
What signals the end of the squares? In this example I assume #48 is the last square to received a wire. How do you know that?
What is the difference between "GL" and "-"?
Guessing, "GL" is Ground Lead & "-" is No Connection.
-
Yes, you're right.
"-" means no connection.