Hi,
I have making this for meter (initially for French Lambert)
Try it, and adjust it if unit is other.
(vl-load-com)
(defun l-coor2l-pt (lst flag / )
(if lst
(cons (list (car lst) (cadr lst) (if flag (caddr lst) 0.0))
(l-coor2l-pt (if flag (cdddr lst) (cddr lst)) flag)
)
)
)
(defun c:ViewPort2Grid ( / js n pt_v l h lst_pt js_obj nw_pl unit_draw AcDoc Space UCS save_ucs WSC nw_style f_pat ob_lst_pt pt_ins format_scale ech hrtx nw_pl_out nw_pl_in hatch_out hatch lst_pt str ori_txt nw_txtpt_ins)
(prin1 "\nSelect a viewport: ")
(while
(null
(setq js
(ssget "_+.:E:S:L"
(list
'(0 . "VIEWPORT")
'(67 . 1)
(cons 410 (getvar "CTAB"))
'(-4 . "!=")
'(69 . 1)
)
)
)
)
)
(setq
pt_v (cdr (assoc 10 (setq dxf_ent (entget (setq ent (ssname js 0))))))
l (cdr (assoc 40 dxf_ent))
h (cdr (assoc 41 dxf_ent))
lst_pt
(list
(list (- (car pt_v) (* 0.5 l)) (- (cadr pt_v) (* 0.5 h)) 0.0)
(list (+ (car pt_v) (* 0.5 l)) (- (cadr pt_v) (* 0.5 h)) 0.0)
(list (+ (car pt_v) (* 0.5 l)) (+ (cadr pt_v) (* 0.5 h)) 0.0)
(list (- (car pt_v) (* 0.5 l)) (+ (cadr pt_v) (* 0.5 h)) 0.0)
)
js_obj (ssadd)
)
(vlax-get (vlax-ename->vla-object ent) 'CustomScale)
(entmakex
(vl-list*
(cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 67 1)
(cons 100 "AcDbPolyline")
(cons 90 (length lst_pt))
(cons 70 1)
(mapcar '(lambda (p) (cons 10 p)) lst_pt)
)
)
(ssadd (setq nw_pl (entlast)) js_obj)
(command "_.CHSPACE" js_obj "")
(setq unit_draw 1000)
(setq AcDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(vla-StartUndoMark AcDoc)
(setq
Space
(if (eq (getvar "CVPORT") 1)
(vla-get-PaperSpace AcDoc)
(vla-get-ModelSpace AcDoc)
)
UCS (vla-get-UserCoordinateSystems AcDoc)
save_ucs
(vla-add UCS
(vlax-3d-point '(0.0 0.0 0.0))
(vlax-3d-point (getvar "UCSXDIR"))
(vlax-3d-point (getvar "UCSYDIR"))
"CURRENT_UCS"
)
)
(vla-put-Origin save_ucs (vlax-3d-point (getvar "UCSORG")))
(setq WCS (vla-add UCS (vlax-3d-Point '(0.0 0.0 0.0)) (vlax-3d-Point '(1.0 0.0 0.0)) (vlax-3d-Point '(0.0 1.0 0.0)) "TEMP_WCS"))
(vla-put-activeUCS AcDoc WCS)
(if (not (tblsearch "STYLE" "$GRID"))
(progn
(setq nw_style (vla-add (vla-get-textstyles AcDoc) "$GRID"))
(mapcar
'(lambda (pr val)
(vlax-put nw_style pr val)
)
(list 'FontFile 'Height 'ObliqueAngle 'Width 'TextGenerationFlag)
(list "SIMPLEX.SHX" 0.0 0.0 1.0 0.0)
)
)
)
(if (not (tblsearch "LAYER" "GRID"))
(vlax-put (vla-add (vla-get-layers AcDoc) "GRID") 'color 7)
)
(if (not (findfile "QUADISO.pat"))
(progn
(setq f_pat (open (strcat (getvar "ROAMABLEROOTPREFIX") "support\\QUADISO.pat") "w"))
(write-line "*QUADISO,Quadrillage lambert" f_pat)
(write-line "0, -.015,0, 0,1, .03,-.97" f_pat)
(write-line "90, 0,-.015, 0,1, .03,-.97" f_pat)
(close f_pat)
)
)
(if (not (findfile "REPQUADISO.pat"))
(progn
(setq f_pat (open (strcat (getvar "ROAMABLEROOTPREFIX") "support\\REPQUADISO.pat") "w"))
(write-line "*REPQUADISO,Repere du quadrillage lambert" f_pat)
(write-line "0, 0,0, 0,1" f_pat)
(write-line "90, 0,0, 0,1" f_pat)
(close f_pat)
)
)
(setq
nw_pl (vlax-ename->vla-object nw_pl)
ob_lst_pt (vlax-get nw_pl 'coordinates)
pt_ins (list (car ob_lst_pt) (cadr ob_lst_pt))
format_scale (/ 1.0 (vlax-get (vlax-ename->vla-object ent) 'CustomScale))
ech (* unit_draw format_scale)
htx (/ ech 500.0)
)
(vla-put-layer nw_pl "GRID")
(vla-Offset nw_pl (* htx 2.5))
(setq nw_pl_out (vlax-ename->vla-object (entlast)))
(vla-Offset nw_pl (+ (* htx 2.5) (* htx 10.0)))
(setq nw_pl_in (vlax-ename->vla-object (entlast)))
(setvar "HPORIGINMODE" 0)
(setvar "HPORIGIN" '(0.0 0.0))
(setq hatch_out (vla-AddHatch Space acHatchPatternTypeCustomDefined "REPQUADISO" :vlax-True))
(vlax-invoke hatch_out 'AppendOuterLoop (list nw_pl))
(vlax-invoke hatch_out 'AppendInnerLoop (list nw_pl_out))
(vla-put-patternscale hatch_out (/ ech 10.0))
(vla-put-patternangle hatch_out 0.0)
(vla-put-layer hatch_out "GRID")
(vla-evaluate hatch_out)
(setq hatch (vla-AddHatch Space acHatchPatternTypeCustomDefined "QUADISO" :vlax-True))
(vlax-invoke hatch 'AppendOuterLoop (list nw_pl_in))
(vla-put-patternscale hatch (/ ech 10.0))
(vla-put-patternangle hatch 0.0)
(vla-put-layer hatch "GRID")
(vla-evaluate hatch)
(setq lst_pt
(l-coor2l-pt
(vlax-invoke
hatch_out
'IntersectWith
nw_pl_out
acExtendThisEntity
)
T
)
)
(foreach el lst_pt
(cond
((or (equal (rem (car el) (/ ech 10.0)) (/ ech 10.0) 1E-8) (equal (rem (car el) (/ ech 10.0)) 0.0 1E-8))
(setq str (strcat " " (rtos (car el) 2 0) " ") ori_txt (* pi 0.5))
)
((or (equal (rem (cadr el) (/ ech 10.0)) (/ ech 10.0) 1E-8) (equal (rem (cadr el) (/ ech 10.0)) 0.0 1E-8))
(setq str (strcat " " (rtos (cadr el) 2 0) " ") ori_txt 0.0)
)
(T (setq str nil ori_txt nil))
)
(cond
((and el str ori_txt)
(setq nw_txt (vla-AddText Space str (vlax-3d-point el) htx))
(vla-put-layer nw_txt "GRID")
(vla-put-StyleName nw_txt "$GRID")
(vla-put-Alignment nw_txt acAlignmentMiddleLeft)
(vla-put-Rotation nw_txt ori_txt)
(vla-put-TextAlignmentPoint nw_txt (vlax-3d-point el))
(if (vlax-invoke nw_pl 'IntersectWith nw_txt acExtendThisEntity)
(vla-put-Alignment nw_txt acAlignmentMiddleRight)
)
)
)
)
(setq pt_ins (polar pt_ins (+ (* pi 0.25) (angle (list (car ob_lst_pt) (cadr ob_lst_pt)) (list (caddr ob_lst_pt) (cadddr ob_lst_pt)))) (* htx 10)))
(setq nw_txt (vla-AddText Space (strcat " Scale 1/" (rtos ech 2 0)) (vlax-3d-point pt_ins) (* 2 htx)))
(vla-put-layer nw_txt "GRID")
(vla-put-StyleName nw_txt "$GRID")
(vla-put-Alignment nw_txt acAlignmentMiddleLeft)
(vla-put-Rotation nw_txt (angle (list (car ob_lst_pt) (cadr ob_lst_pt)) (list (caddr ob_lst_pt) (cadddr ob_lst_pt))))
(vla-put-TextAlignmentPoint nw_txt (vlax-3d-point pt_ins))
(and save_ucs (vla-put-activeUCS AcDoc save_ucs))
(and WCS (vla-delete WCS) (setq WCS nil))
(vla-EndUndoMark AcDoc)
(prin1)
)