Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

lisp function not working correctley, help?

19 REPLIES 19
Reply
Message 1 of 20
Anonymous
623 Views, 19 Replies

lisp function not working correctley, help?

Please find the attached files for reference.

I have a code that labels the ex grade and the pipe ie.

The code ask the user to pick the base elevation, the profile, the label start and end location, and to mirror or move the label. It is all working but for some reason the elevations are not correct, can someone tell me what is wrong?

Thanks

19 REPLIES 19
Message 2 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....

I have a code that labels the ex grade and the pipe ie.

The code ask the user to pick the base elevation, the profile, the label start and end location, and to mirror or move the label. It is all working but for some reason the elevations are not correct, can someone tell me what is wrong?

....



I didn't look too deeply, and don't fully understand everything that it's doing, but I have a guess....

 

You are setting the UCS to World at the beginning, with this:

 

(command "ucs" "save" "PPL")
(command "ucs" "")

 
to be reset back at the end.  But then later you have this line:

 

(setq startpt (trans (car (vl-remove-if '(lambda (x) (< (car a) (car b))) grcoords)) 0 1))

 

which appears to be translating a point [which I think is used in calculating what's coming out wrong] from the World Coordinate System to the current Coordinate System.  But the current CS is the WCS, so the (trans) function won't change anything.

 

I suspect that maybe you should not set the Coordinate System to WCS.

Kent Cooper, AIA
Message 3 of 20
Anonymous
in reply to: Kent1Cooper

I tried that but no luck. I also tried to remove the Trans

(setq startpt (car (vl-remove-if '(lambda (x)(< (car a)(car b)))grcoords)))

and i get the same result.

I wounder if it has something to do with this line

(setq grcoords (vl-remove-if 'not (mapcar '(lambda (x)(if (= 10 (car x))(cdr x)))(entget grad))))

because If i go threw the code step by step and check the values the coordinate list that this returns is not on the exgrade line. Do you know of a better way to get the elevations, or can you dig a little deeper because i have worked on this all week and am having no luck figuring it out. Thanks  

 

 

Message 4 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....

(setq grcoords (vl-remove-if 'not (mapcar '(lambda (x)(if (= 10 (car x))(cdr x)))(entget grad))))

because If i go threw the code step by step and check the values the coordinate list that this returns is not on the exgrade line. ....


For me, that returns a correct list of WCS XY coordinates when applied to a Polyline whose entity name is stored in the 'grad' variable.  Could it be not setting the right Polyline into that variable?

Kent Cooper, AIA
Message 5 of 20
Anonymous
in reply to: Kent1Cooper

No it is getting the correct entity and it returns a list but if you plot the coords manually by drawing a line it does not correspond to a point on the line, I am guessing it should. Please open a new dwg file and draw a polyline on the layer  C-ROAD-PROF, then run the code below. If you plot the coords are they on the line you drew? Maybe they are not soposto be but seems like they should??????

 

(prompt "\nWindow arond the line you just drew:")
(setq p1 (getpoint "\nSpecify first corner: "));Get point from user to use (ssget "W" P1 P2
(setq p2 (getcorner p1 "\nSpecify opposite corner: "))

(setq grad (ssget "W" P1 P2 (list (cons 0 "lwpolyline")(cons 8 "C-ROAD-PROF"))))

(setq grad (ssname grad 0))

(setq grcoords (vl-remove-if 'not (mapcar '(lambda (x)(if (= 10 (car x))(cdr x)))(entget grad))))

Message 6 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

No it is getting the correct entity and it returns a list but if you plot the coords manually by drawing a line it does not correspond to a point on the line, I am guessing it should. Please open a new dwg file and draw a polyline on the layer  C-ROAD-PROF, then run the code below. If you plot the coords are they on the line you drew? Maybe they are not soposto be but seems like they should??????

....


It returns the correct list of coordinates for me.  Are you sure you're in the WCS?

Kent Cooper, AIA
Message 7 of 20
Anonymous
in reply to: Anonymous

I created a brand new dwg and drew a polyline with 4 vertexes (beginning middle middle end) then I listed the vertexes of that line abd the coords are not the same. I am positive I am in world cordinance
Message 8 of 20
Anonymous
in reply to: Anonymous

Can anyone help me? Kent1Cooper you still there? Any ideas?

Message 9 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

Can anyone help me? Kent1Cooper you still there? Any ideas?


Yes, I'm still here.  If listing the coordinates gives you different values than when you drew the Polyline, I don't have any other ideas.  If drawing another Polyline with those coordinates gives you vertices in different places, the only other thing I can think of is that maybe you have running Object Snap modes that are finding locations to snap to on other objects.

Kent Cooper, AIA
Message 10 of 20
Anonymous
in reply to: Kent1Cooper

Please see attachment.

Can you look at the file and tell me if you get the same coords as i do when?

 

All i know is that this is not working so can you tell me how to write the code in a different way so that it does work?

 

Message 11 of 20
CADaSchtroumpf
in reply to: Anonymous

If i make this

 

(mapcar 'list (mapcar 'rtos (mapcar 'car grcoords)) (mapcar 'rtos (mapcar 'cadr grcoords)))

 

The coords are the same that returned by the command ID on the red pline !

 the line code:

(setq grcoords (vl-remove-if 'not (mapcar '(lambda (x)(if (= 10 (car x))(cdr x)))(entget (car (entsel))))))

is right

Message 12 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

Please see attachment.

Can you look at the file and tell me if you get the same coords as i do when?

 

All i know is that this is not working so can you tell me how to write the code in a different way so that it does work?

 


I may look deeper later [time to go home], and I haven't run the code on that yet.  But one thing I notice is that the white Lines all have their endpoints on whole-number values in both X & Y directions [set Snap to 1 and you'll see that the cursor hits the endpoints, or turn on the Grid], but the red Polyline doesn't have its vertices positioned that way.  Something seems to be rounding things off, but only the first two are at the nearest integer-coordinates location, so I don't know what's going on.  Could it be a limit on number of significant figures?  Does it do the same if you move the red one close to 0,0 instead of having the coordinate values so high?

Kent Cooper, AIA
Message 13 of 20
Anonymous
in reply to: CADaSchtroumpf

Ok yes you are correct if i use that it does give the correct coords but the how do i add that to the code so it will annotate the elevations correctly? In Fact this may not be the problem at all, do you have any idea what is causing it not to work?

Message 14 of 20
CADaSchtroumpf
in reply to: Anonymous

Kent give me a way for your possible error.

 

In your exemple if i active SNAPMODE, the cursor stay on white vertex.

I don't look your code in deep, but i presume that observation of SNAPUNIT is in correlation with your problem.

Message 15 of 20
Anonymous
in reply to: Kent1Cooper

You may be on to something here, If i move the example3 dwg file close to 0,0 then yes i get the correct coords.

But if i take example2.dwg and move it to 0,0 the code returns the correct coords but the elevations are still wrong, this leads me to think it is some thing else.

Message 16 of 20
Anonymous
in reply to: Anonymous

I have now stepped threw the code and i think the problem comes into play here
(foreach p ptlist
  (setq lp  (polar p (* pi 0.75) 2.47)
 rp  (polar p (/ pi 4) 2.47))
  (setq dp (vlax-curve-getclosestpointto grad p)
 pp (vlax-curve-getclosestpointto prof p))
  (setq dlev (cadr dp)
 plev (cadr pp))
 
   (setq txt (vla-addtext  acsp (strcat "E.G. EL="(rtos (+ zero (/ (- dlev(cadr tlinept) ) 10.)) 2 2)) (vlax-3d-point lp) 2.54))
      (vla-put-alignment txt  acAlignmentMiddleLeft )
      (vla-put-textalignmentpoint txt (vlax-3d-point lp))
               (vla-put-rotation txt (/ pi 2))
  (vla-put-color txt acred)
  (vla-put-stylename txt  "HGBD-OPTI1-MS" )
  (setq txt (vla-addtext  acsp (strcat "PIPE I.E.="(rtos (+ zero (- (/ (- plev (cadr tlinept) ) 10.) (/ (/ dia 1000.) 2))) 2 2)) (vlax-3d-point rp) 2.54))
      (vla-put-alignment txt  acAlignmentMiddleLeft )
      (vla-put-textalignmentpoint txt (vlax-3d-point rp))
               (vla-put-rotation txt (/ pi 2))
  (vla-put-color txt acyellow)
  (vla-put-stylename txt  "HGBD-OPTI1-MS" )
  )

I do not understand what this line of the code does
(polar p (* pi 0.75) 2.47)
(polar p (/ pi 4) 2.47))
or the values it returns, I looked at the help but i still dont understand what it does

Seams like using (vlax-curve-getclosestpointto grad p) could be a problem because it only gets a point closest to a curve, and in this case there are no curves

Message 17 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

I have now stepped threw the code and i think the problem comes into play here
(foreach p ptlist

....
  (setq dp (vlax-curve-getclosestpointto grad p)
 pp (vlax-curve-getclosestpointto prof p))
....

 

I do not understand what this line of the code does
(polar p (* pi 0.75) 2.47)
(polar p (/ pi 4) 2.47))
or the values it returns, I looked at the help but i still dont understand what it does

Seams like using (vlax-curve-getclosestpointto grad p) could be a problem because it only gets a point closest to a curve, and in this case there are no curves



Keeping in mind that AutoLISP handles all angles in radians, look again at the AutoLISP Reference about (polar).  It's just finding two points that are 2.47 units from p in the 135- and 45-degree directions.

 

The term "curve" doesn't need to mean things that are curved by the general definition -- in (vlax-curve...) functions it applies also to straight things.  Anything with linearity [Line, Polyline, Arc, Circle, Ellipse, Spline, etc.] qualifies as a "curve," but not things without it, like Text.

 

If I'm reading the code right, I agree that the problem is most likely with the (vlax-curve-getClosestPointTo) function, but not because the Polylines don't count as "curves."  I think the points represented by 'p' in the 'ptlist' are horizontally aligned in relation to the 'tline' which I think is along the base of the graph.  The closest point on a sloped Polyline [either the grad or prof variable will usually, if not always, be sloped at varying angles] to a given point will not be directly above that given point, but will be the base of a perpendicular from the point to the Polyline, which perpendicular will usually not be vertical.  I suspect you want the point that is directly vertically above the 'p' location on the Polyline.  That could be found by drawing a temporary vertical Line through the p location, and using the (vla-intersectwith) function to find where that vertical intersects the Polyline.  That function is not documented in the AutoLISP Reference, but do a Search for that here, and you'll find lots of examples.

Kent Cooper, AIA
Message 18 of 20
Anonymous
in reply to: Kent1Cooper

Yes you are correct the tline is at the base of the graph, and yes i want location that is vertical to the p location, so i will look into the function and see if i can figure out how to draw a temp line.

 

I was thinking that maybe a way to do this would be to get the list intersetions along the grad and prof variable and the vertical grid lines, but i dont know how to select the lines at angel 90 on the layer C-ROAD-PROF-GRID-MAJR.

 

Thanks for all the help, I am doing as much as i can trying to learn this stuff but i think this code is over my head.

Message 19 of 20
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

Yes you are correct the tline is at the base of the graph, and yes i want location that is vertical to the p location, so i will look into the function and see if i can figure out how to draw a temp line.

 

I was thinking that maybe a way to do this would be to get the list intersetions along the grad and prof variable and the vertical grid lines, but i dont know how to select the lines at angel 90 on the layer C-ROAD-PROF-GRID-MAJR.

....


Finding those Lines would be easy -- just add another (ssget) after the first two here that are already in your code:

 

  (setq grad (ssget "W" P1 P2 (list (cons 0 "lwpolyline")(cons 8 "C-ROAD-PROF"))));selects the gradeline
  (setq prof (ssget "W" P1 P2 (list (cons 0 "lwpolyline")(cons 8 "C-SSWR-FORC"))));selects the forcemain line
  (setq verts (ssget "W" P1 P2 '((0 . "LINE") (8 . "C-ROAD-PROF-GRID-MAJR"))));selects the major grid lines

 

But that would get you all of them, including the horizontal ones [there's no data on the angle that you can filter by].  The collection could be pared down to just the vertical ones easily enough, with a check on whether the X components of their endpoints are equal.  This approach would also eliminate the need to ask the User for the first and last label locations, and [I think] the 'tline' and probably some other elements.  I can't work that out further right now, but I may look at it again later.

Kent Cooper, AIA
Message 20 of 20
Anonymous
in reply to: Kent1Cooper

I got it working!!!!!!!!!!

The problem was vlax-curve... I did like you said temp line and get intersection

THANKS FOR ALL THE HELP!!

 (foreach p ptlist
  (setq lp  (polar p (* pi 0.75) 2.47)
 rp  (polar p (/ pi 4) 2.47))
 
  (command "._line" P "@800<90" "")
  (setq templine (ssget "_l"))
  (setq templine (ssname templine 0))
(setq vevo1 (vlax-ename->vla-object grad))
(setq vevo1a (vlax-ename->vla-object prof))
(setq vevo2 (vlax-ename->vla-object templine))
(setq dp (vlax-invoke vevo1 'IntersectWith vevo2 1))
(setq pp (vlax-invoke vevo1a 'IntersectWith vevo2 1))
(setq dlev (cadr dp)
      plev (cadr pp))
     (command "erase" templine "")

 

As i finish this up i may have some more questions but the big one is done

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost