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

quick Drawing Grid ?

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
4626 Views, 8 Replies

quick Drawing Grid ?

Would it be possible to select all lines in a drawing and move them onto the
drawing grid?

J. Logan
8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: Anonymous

I should add that the spacing of the lines or the distance they are "off"
from the grid would not be equal.


"J. Logan" wrote in message
news:5396921@discussion.autodesk.com...
Would it be possible to select all lines in a drawing and move them onto the
drawing grid?

J. Logan
Message 3 of 9
Anonymous
in reply to: Anonymous

> "J. Logan" wrote in message
> news:5396921@discussion.autodesk.com...
> Would it be possible to select all lines in a drawing and move them onto the
> drawing grid?

J. Logan wrote:
> I should add that the spacing of the lines or the distance they are
> "off" from the grid would not be equal.

I interpret this as wanting to snap the endpoints of the lines to the
grid after they've already been drawn.

Try this, only works on lines:

;;; Command: SNAP2GRID
;;;
;;; prompts for a selection of LINE entities,
;;; modifies them such that their endpoints
;;; fall on GRID points
;;;
;;; requires NearestGridPoint function
;;;
;;; written by Adam Wuellner
;;;
(defun c:snap2grid (/ ss c ent)
(setq ss (ssget '((0 . "LINE")))
c 0)
(repeat (sslength ss)
(setq ent (entget (ssname ss c))
c (1+ c))
(entmod
(subst
(cons 10 (NearestGridPoint (cdr (assoc 10 ent))))
(assoc 10 ent)
(subst
(cons 11 (NearestGridPoint (cdr (assoc 11 ent))))
(assoc 11 ent)
ent))))
(princ))

;;; Function: NearestGridPoint
;;;
;;; Returns a point representing the grid point
;;; nearest to the point passed to the function.
;;;
;;; expects: a point in WCS
;;; returns: a point in WCS
;;;
;;; written by Adam Wuellner
;;;
(defun NearestGridPoint (pt-WCS / pt-UCS
X-UCS Y-UCS Z-UCS
gridSpacingX gridSpacingY
OffsetPt)
(setq gridSpacingX (car (getvar 'GRIDUNIT))
gridSpacingY (cadr (getvar 'GRIDUNIT))
OffsetPt (mapcar 'rem
(getvar 'SNAPBASE)
(getvar 'GRIDUNIT))
pt-UCS (trans pt-WCS 0 1)
X-UCS (car pt-UCS)
Y-UCS (cadr pt-UCS))
(if (equal 3 (length pt-WCS))
(setq Z-UCS (caddr pt-UCS)))
(setq X-UCS (* gridSpacingX
(/ (abs X-UCS) X-UCS)
(fix (/ (+ (abs X-UCS) (/ gridSpacingX 2.0))
gridSpacingX)))
Y-UCS (* gridSpacingY
(/ (abs Y-UCS) Y-UCS)
(fix (/ (+ (abs Y-UCS) (/ gridSpacingY 2.0))
gridSpacingY)))
pt-UCS (mapcar '+ (list X-UCS Y-UCS) OffsetPt))
(if Z-UCS
(setq pt-UCS (append pt-UCS (list Z-UCS))))
(trans pt-UCS 1 0))
Message 4 of 9
Anonymous
in reply to: Anonymous

Yes, that is what I'm asking. I was beginning to think I wasn't asking it
right.
Thanks Adam I'll take a look at this.


"Adam Wuellner" wrote in message
news:5397296@discussion.autodesk.com...
> "J. Logan" wrote in message
> news:5396921@discussion.autodesk.com...
> Would it be possible to select all lines in a drawing and move them onto
the
> drawing grid?

J. Logan wrote:
> I should add that the spacing of the lines or the distance they are
> "off" from the grid would not be equal.

I interpret this as wanting to snap the endpoints of the lines to the
grid after they've already been drawn.

Try this, only works on lines:

;;; Command: SNAP2GRID
;;;
;;; prompts for a selection of LINE entities,
;;; modifies them such that their endpoints
;;; fall on GRID points
;;;
;;; requires NearestGridPoint function
;;;
;;; written by Adam Wuellner
;;;
(defun c:snap2grid (/ ss c ent)
(setq ss (ssget '((0 . "LINE")))
c 0)
(repeat (sslength ss)
(setq ent (entget (ssname ss c))
c (1+ c))
(entmod
(subst
(cons 10 (NearestGridPoint (cdr (assoc 10 ent))))
(assoc 10 ent)
(subst
(cons 11 (NearestGridPoint (cdr (assoc 11 ent))))
(assoc 11 ent)
ent))))
(princ))

;;; Function: NearestGridPoint
;;;
;;; Returns a point representing the grid point
;;; nearest to the point passed to the function.
;;;
;;; expects: a point in WCS
;;; returns: a point in WCS
;;;
;;; written by Adam Wuellner
;;;
(defun NearestGridPoint (pt-WCS / pt-UCS
X-UCS Y-UCS Z-UCS
gridSpacingX gridSpacingY
OffsetPt)
(setq gridSpacingX (car (getvar 'GRIDUNIT))
gridSpacingY (cadr (getvar 'GRIDUNIT))
OffsetPt (mapcar 'rem
(getvar 'SNAPBASE)
(getvar 'GRIDUNIT))
pt-UCS (trans pt-WCS 0 1)
X-UCS (car pt-UCS)
Y-UCS (cadr pt-UCS))
(if (equal 3 (length pt-WCS))
(setq Z-UCS (caddr pt-UCS)))
(setq X-UCS (* gridSpacingX
(/ (abs X-UCS) X-UCS)
(fix (/ (+ (abs X-UCS) (/ gridSpacingX 2.0))
gridSpacingX)))
Y-UCS (* gridSpacingY
(/ (abs Y-UCS) Y-UCS)
(fix (/ (+ (abs Y-UCS) (/ gridSpacingY 2.0))
gridSpacingY)))
pt-UCS (mapcar '+ (list X-UCS Y-UCS) OffsetPt))
(if Z-UCS
(setq pt-UCS (append pt-UCS (list Z-UCS))))
(trans pt-UCS 1 0))
Message 5 of 9
Anonymous
in reply to: Anonymous

"Adam Wuellner" wrote in message
news:5397296@discussion.autodesk.com...

>>I interpret this as wanting to snap the endpoints of the lines to the
>>grid after they've already been drawn.

>>Try this, only works on lines:

Adam, I'm assuming you tried to make it work with polylines? I just tried to
add polylines into the...

(setq ss (ssget '((0 . "LINE,POLYLINE")))

with no success. What do you think is the reasoning for it not working?

J. Logan
Message 6 of 9
Anonymous
in reply to: Anonymous

J. Logan wrote:
> "Adam Wuellner" wrote in message
> news:5397296@discussion.autodesk.com...
>
>>> I interpret this as wanting to snap the endpoints of the lines to the
>>> grid after they've already been drawn.
>
>>> Try this, only works on lines:
>
> Adam, I'm assuming you tried to make it work with polylines? I just tried to
> add polylines into the...
>
> (setq ss (ssget '((0 . "LINE,POLYLINE")))
>
> with no success. What do you think is the reasoning for it not working?

It's the DXF codes. For lines, we only need to be concerned with DXf
codes 10 and 11, which are the start point and the end point,
respectively. For polylines, you'd need to get all the vertices, which
are stored differently depending on the sort of polyline you've got. So
the method to adjust lines is simply:

1. select the line
2. get the start point
3. get the end point
4. calculate the grid point nearest to the start point
5. calculate the grid point nearest to the end point
6. modify the line using the computed values for start pt and end pt

For a polyline, it would be:
1. select the polyline
2. for each vertex
a. get the vertex point
b. calculate the grid point nearest to the vertex point
c. move the vertex to the calculated point

It's not necessarily more complex, but it's different. And how it's
done might have to be different, too, depending on if each selected
pline is an old "heavy" pline or one of the newer lightweight polylines,
since each of those objects uses a different DXF structure for storing
its vertices.

So the command function, SNAP2GRID, would need to be rewritten with
three major branches - one for processing lines, one for processing
lwpolylines, and one for the older polylines. If any other object types
would need to be handled (arcs, maybe?) then you'd likely need to code
around the particular DXF structure of each type of object and how you
want to adjust them.

I didn't give it a shot, but that helper function NearestGridPoint can
be used to calculate the new points, so you'd just need to figure out
how to loop through and adjust the vertices using the values returned
from that function.
Message 7 of 9
Anonymous
in reply to: Anonymous

"Adam Wuellner" wrote in message
news:5397542@discussion.autodesk.com...
J. Logan wrote:
> "Adam Wuellner" wrote in message
> news:5397296@discussion.autodesk.com...

>I didn't give it a shot, but that helper function NearestGridPoint can
>>be used to calculate the new points, so you'd just need to figure out
>>how to loop through and adjust the vertices using the values returned
>>from that function.

Oddly enough I understand 90% of what you're saying. If I'm not mistaken Joe
Burke has posted
something for the loop issue. I'll do a search tomorrow.

Thanks again Adam. Mucho appreciated

J. Logan
Message 8 of 9
Anonymous
in reply to: Anonymous

J. Logan,

I did a search for the FixOffGrid routine I posted back in April, but couldn't find
it. File attached.

It handles various object types, including polylines. Read the header comments. I
hope they are clear.

Joe Burke
Message 9 of 9
Anonymous
in reply to: Anonymous

Thanks Joe. I'll take a look at it. I didn't realize you had actually
addressed the grid issue directly.
I was thinking more of the bulge (?) routine you created a while back. Nice
to know I don't have to muddle too
much.

Thanks again.

J. Logan




"Joe Burke" wrote in message
news:5397909@discussion.autodesk.com...
J. Logan,

I did a search for the FixOffGrid routine I posted back in April, but
couldn't find
it. File attached.

It handles various object types, including polylines. Read the header
comments. I
hope they are clear.

Joe Burke

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report