Article | 41024172 |
Type | HowTo |
Product | Engine |
Version | 5.1.1.1039 |
Date Added | 12/14/2005 |
Submitted by | Danny Watson |
Keywords |
The folowing source code adds vertical and horizontal rullers in vector draw. The rullers are drawn at DrawAfterAll event.
We use seperate loops to draw lines and texts becuse setting the pen color each time is more time expensive than using diferent loops.
Visual Basic 6 Source code
Public Function fMod(x As Double, y As Double) As Double
Dim a As Double
Dim I As Long
a = x / y
I = Int(a)
fMod = x - I * y
End Function
Private Sub VD_DrawOverAll(ByVal Render As VDProLib5.vdRender, Cancel As Integer)
Dim PixSize As Double, Step As Double, RullerUnitLen As Double, RullerSubUnitLen As Double, subStep As Double
Dim x1 As Double, y1 As Double, z1 As Double, w As Double, h As Double, mx As Double, my As Double
Dim x As Double, y As Double, xx As Double, yy As Double
Dim Mat As vdmatrix
Dim Pt As Variant
Dim Str As String
PixSize = Render.PixelSize
Step = 1#
RullerUnitLen = 16
RullerSubUnitLen = 8
subStep = Step / 10#
While subStep < PixSize * 2#
Step = Step * 2#
subStep = Step / 10#
Wend
'Enable the mouse snap and set it to the step calculated
VD.ActiveDocument.SetSnapSpace subStep, subStep
VD.ActiveDocument.SnapMode = True
'Calculate the LowerLeft and the Upperlesft in WorldCS
Render.GetViewCenter x1, y1, z1
Set Mat = Render.Matrix: Mat.Inverse
Pt = Mat.TransformPt(Array(x1, y1, z1))
x1 = Pt(0): y1 = Pt(1): z1 = Pt(2)
w = Render.Width * PixSize: h = Render.Height * PixSize
mx = x1 - w / 2#: my = y1 - h / 2#
y = my - fMod(my, subStep)
Render.Select_Pen 0, 7, 0 'DRAW LINE IN y AXIS
yy = y
While yy < y + h - RullerUnitLen * PixSize
Render.Moveto mx, yy, 0
If (Abs(fMod(yy, Step)) < 0.001) Or _
(Abs(Abs(fMod(yy, Step)) - Abs(Step)) < 0.001) Then
Render.Lineto mx + RullerUnitLen * PixSize, yy, 0
Else
Render.Lineto mx + RullerSubUnitLen * PixSize, yy, 0
End If
yy = yy + subStep
Wend
Render.Select_Pen 0, 1, 0 ' DRAW TEXTS IN y AXIS WITH DIFFERENT COLOR
yy = y
While yy < y + h - RullerUnitLen * PixSize
If (Abs(fMod(yy, Step)) < 0.001) Or _
(Abs(Abs(fMod(yy, Step)) - Abs(Step)) < 0.001) Then
Str = VD.Utility.LReal2StringUnits(yy, VD.ActiveDocument.Lunits, VD.ActiveDocument.Luprec)
Render.Drawtext Str, mx + (RullerUnitLen + 2) * PixSize, yy, 0, VD.ActiveDocument.TextStyles.Item(0), 8 * PixSize, 0, 0, 100
End If
yy = yy + subStep
Wend
my = my + h: mx = mx + RullerUnitLen * PixSize: x = mx - fMod(mx, subStep)
Render.Select_Pen 0, 7, 0 'DRAW LINE IN x AXIS
xx = x
While xx < y + w
Render.Moveto xx, my, 0
If (Abs(fMod(xx, Step)) < 0.001) Or _
(Abs(Abs(fMod(xx, Step)) - Abs(Step)) < 0.001) Then
Render.Lineto xx, my - RullerUnitLen * PixSize, 0
Else
Render.Lineto xx, my - RullerSubUnitLen * PixSize, 0
End If
xx = xx + subStep
Wend
Render.Select_Pen 0, 2, 0 'DRAW TEXTS IN x AXIS WITH DIFFERENT COLOR
xx = x
While xx < y + w
Render.Moveto xx, my, 0
If (Abs(fMod(xx, Step)) < 0.001) Or _
(Abs(Abs(fMod(xx, Step)) - Abs(Step)) < 0.001) Then
Str = VD.Utility.LReal2StringUnits(xx, VD.ActiveDocument.Lunits, VD.ActiveDocument.Luprec)
Render.Drawtext Str, xx, my - (RullerUnitLen + 2) * PixSize, 0, VD.ActiveDocument.TextStyles.Item(0), 8 * PixSize, 3.1415927 / 2#, 2, 100
End If
xx = xx + subStep
Wend
End Sub
C# .net Source code
private void axVDPro1_DrawOverAll(object sender, AxVDProLib5._DVdrawEvents_DrawOverAllEvent e)
{
double pixsize,step,rullerUnitLen,rullerSubUnitLen,substep;
double x1=0,y1=0,z1=0;
double w,h,mx,my,x,y,yy,xx;
vdmatrix mat;
double[] pt;
string str;
pixsize=e.render.PixelSize;
step=1.0;
rullerUnitLen=16.0;
rullerSubUnitLen=8.0;
substep=step/10.0;
//change step according to zoom
while(substep < pixsize*2.0)
{
step*=2.0;
substep=step/10.0;
}
//enable mouse to snap on calculated step
axVDPro1.ActiveDocument.SetSnapSpace(substep,substep);
axVDPro1.ActiveDocument.SnapMode=true;
//calculate the lower left my and uper left mx in wolrd coordinates
e.render.GetViewCenter(ref x1, ref y1, ref z1);
mat=e.render.Matrix;
mat.Inverse();
pt=(double[])mat.TransformPt(new double[]{x1,y1,z1});
x1=pt[0];
y1=pt[1];
z1=pt[2];
w=(double)e.render.Width*pixsize;
h=(double)e.render.Height*pixsize;
mx=x1-w/2.0;
my=y1-h/2.0;
y=my-Math.IEEERemainder(my,substep);
e.render.Select_Pen(VdConstPen.VdPenSolid,7,0); //draw lines on Y axis using forground color
for(yy = y; yy < y + h - rullerUnitLen * pixsize; yy+=substep)
{
e.render.Moveto(mx,yy,0);
if(Math.Abs(Math.IEEERemainder(yy,step))<0.00000001 ||
Math.Abs(Math.Abs(Math.IEEERemainder(yy,step))-Math.Abs(step))<0.00000001)
e.render.Lineto(mx+rullerUnitLen*pixsize,yy,0);
else
e.render.Lineto(mx+rullerSubUnitLen*pixsize,yy,0);
}
e.render.Select_Pen(VdConstPen.VdPenSolid,1,0); //draw text on Y axis using forground color
for(yy = y; yy < y + h - rullerUnitLen * pixsize; yy+=substep)
{
//e.render.Moveto(mx,yy,0);
if(Math.Abs(Math.IEEERemainder(yy,step)) < 0.00000001||
Math.Abs(Math.Abs(Math.IEEERemainder(yy,step))-Math.Abs(step))<0.00000001)
{
//The ruller text is formated using the ActiveDocument Lunits and Luprec properties
str=axVDPro1.Utility.LReal2StringUnits(yy,axVDPro1.ActiveDocument.Lunits,axVDPro1.ActiveDocument.Luprec);
e.render.Drawtext(str,mx+(rullerUnitLen+2)*pixsize,yy,0,null,8*pixsize,0,VdConstHorJust.VdTextHorLeft,VdConstVerJust.VdTextVerCen);
}
}
my=my+h;
mx+=rullerUnitLen*pixsize;
x=mx-Math.IEEERemainder(mx,substep);
e.render.Select_Pen(VdConstPen.VdPenSolid,7,0); //draw lines on X axis using forground color
for(xx=x; xx< x+w;xx+=substep)
{
e.render.Moveto(xx,my,0);
if(Math.Abs(Math.IEEERemainder(xx,step))<0.00000001||
Math.Abs(Math.Abs(Math.IEEERemainder(xx,step))-Math.Abs(step))<0.00000001)
e.render.Lineto(xx,my-rullerUnitLen*pixsize,0);
else
e.render.Lineto(xx,my-rullerSubUnitLen*pixsize,0);
}
e.render.Select_Pen(VdConstPen.VdPenSolid,2,0); //draw text on X axis using forground color
for(xx =x; xx<x+w;xx+=substep)
{
//e.render.Moveto(xx,my,0);
if(Math.Abs(Math.IEEERemainder(xx,step))<0.00000001||
Math.Abs(Math.Abs(Math.IEEERemainder(xx,step))-Math.Abs(step)) < 0.00000001)
{
//The ruller text is formated using the ActiveDocument Lunits and Luprec properties
str=axVDPro1.Utility.LReal2StringUnits(xx,axVDPro1.ActiveDocument.Lunits,axVDPro1.ActiveDocument.Luprec);
e.render.Drawtext(str,xx,my-(rullerUnitLen+2)*pixsize,0,null,8*pixsize,Math.PI/2.0,VdConstHorJust.VdTextHorRight,VdConstVerJust.VdTextVerCen);
}
}
}
C++ VisualStudio 6 Source code
void CVdView::OnDrawOverAllvdrawCTRL2(LPDISPATCH Render, short FAR* Cancel)
{
// TODO: Add your control notification handler code here
double pixsize,step,rullerUnitLen,rullerSubUnitLen,substep;
double x1,y1,z1,w,h,mx,my,x,y,yy,xx;
Cvdmatrix mat;
COlePoint pt;
CString str;
CvdRender rend;
rend.AttachDispatch(Render,FALSE);
pixsize = rend.GetPixelSize();
step = 1.0;
rullerUnitLen = 16;
rullerSubUnitLen = 8;
substep = step / 10;
//change the step depending on zoom
while(substep < pixsize*2){
step *= 2;
substep = step / 10;
}
//enable the mouse snap in the calculating step
m_Vdraw.GetActiveDocument().SetSnapSpace(substep,substep);
m_Vdraw.GetActiveDocument().SetSnapMode(TRUE);
//calculate the lower left my and upper left mx in World coordinates
rend.GetViewCenter(&x1,&y1,&z1);
mat = rend.GetMatrix();
mat.Inverse();
pt = mat.TransformPt(COlePoint(x1,y1,z1));
x1 = pt[0];
y1 = pt[1];
z1 = pt[2];
w = rend.GetWidth() * pixsize;
h = rend.GetHeight() * pixsize;
mx = x1 - w / 2.0;
my = y1 - h / 2.0;
y = my - fmod(my , substep);
rend.Select_Pen(0,7,0);//draw lines on Y axis using forground color
for(yy = y ; yy < y + h - rullerUnitLen*pixsize; yy += substep){
rend.Moveto(mx,yy,0);
if(equal(fmod(yy , step),0.0,1.0e-08) ||
equal(fabs(fmod(yy , step)),fabs(step),1.0e-08)
){
rend.Lineto(mx + rullerUnitLen*pixsize,yy,0);
}
else
rend.Lineto(mx + rullerSubUnitLen*pixsize,yy,0);
}
rend.Select_Pen(0,7,0);//draw text on Y axis using forground color
for( yy = y ; yy < y + h - rullerUnitLen*pixsize; yy += substep){
if(equal(fmod(yy , step),0.0,1.0e-08)||
equal(fabs(fmod(yy , step)),fabs(step),1.0e-08)
){
//the ruller text is formatting using the ActiveDocument Lunits and Luprec properties
str = m_Vdraw.GetUtility().LReal2StringUnits(yy,m_Vdraw.GetActiveDocument().GetLunits(),m_Vdraw.GetActiveDocument().GetLuprec());
rend.Drawtext(str,mx + (rullerUnitLen + 2)*pixsize ,yy,0,NULL,8*pixsize,0,0,100);
}
}
my = my + h;
mx += rullerUnitLen*pixsize;
x = mx - fmod(mx, substep);
rend.Select_Pen(0,7,0);//draw lines on X axis using forground color
for(xx = x ; xx < x + w; xx += substep){
rend.Moveto(xx,my,0);
if(equal(fmod(xx , step),0.0,1.0e-08)||
equal(fabs(fmod(xx , step)),fabs(step),1.0e-08)
){
rend.Lineto(xx,my - rullerUnitLen*pixsize,0);
}
else
rend.Lineto(xx,my - rullerSubUnitLen*pixsize,0);
}
rend.Select_Pen(0,7,0);;//draw text on X axis using forground color
for( xx = x ; xx < x + w; xx += substep){
rend.Moveto(xx,my,0);
if(equal(fmod(xx , step),0.0,1.0e-08)||
equal(fabs(fmod(xx , step)),fabs(step),1.0e-08)
){
//the ruller text is formatting using the ActiveDocument Lunits and Luprec properties
str = m_Vdraw.GetUtility().LReal2StringUnits(xx,m_Vdraw.GetActiveDocument().GetLunits(),m_Vdraw.GetActiveDocument().GetLuprec());
rend.Drawtext(str,xx,my - (rullerUnitLen + 2)*pixsize,0,NULL,8*pixsize,PI/2.0,2,100);
}
}
return;