问题描述
我已经尝试了一个星期来绘制一个奇怪的形状,然后在尝试使用下面的代码时通过说.1使其正确变大,我得到了以下结果:
如何以正确的宽高比“勾画”图形?
代码:
foreach (clsdisplayList p in mdisplayLists)
{
if (!p.InView)
{
continue;
}
if (p.Rapid)
{
mCurPen.DashStyle = System.Drawing.drawing2d.DashStyle.Custom;
mCurPen.DashPattern = mRapidDashStyle;
}
else
{
mCurPen.DashStyle = System.Drawing.drawing2d.DashStyle.solid;
}
path.AddLines(p.Points);
mCurPen.Color = p.Color;
g.DrawLines(mCurPen,p.Points);
}
g.ResetTransform();
g.MultiplyTransform(mMtxDraw);
g.TranslateTransform(0,1);
//make new matrix
Matrix newpath = new Matrix();
newpath.Translate(0,1);
newpath.Scale(1.1f,1.1f);
path.Transform(newpath);
g.DrawPath(mCurPen,path); //tring to scale here
此代码基于名为cnc viewer的项目。我基本上是试图从gcode的角度获取形状,然后将其过大,以便可以为钻孔点修改gcode。我已经编写了一个编写gcode的程序,但是还没有弄清楚如何对其进行回填以使我能看到所需的坐标。我已按要求添加了缩放代码。内部形状是正确的,但我需要将外部形状加大2英寸。绘制初始形状后,我正在绘制“外部形状”。 https://www.codeproject.com/Articles/17424/CNC-Graphical-Backplotter
public void WindowViewport(float X1,float Y1,float X2,float Y2)
{
float temp = 0;
//convert window from right to left
if ((X1 > X2))
{
temp = X2;
X2 = X1;
X1 = temp;
}
//convert window from bottom to top
if ((Y1 > Y2))
{
temp = Y2;
Y2 = Y1;
Y1 = temp;
}
if (Math.Abs(X2 - X1) < 0.01)
{
return;
}
if (Math.Abs(Y2 - Y1) > 1000)
{
return;
}
mViewRect.X = X1;
mViewRect.Y = Y1;
mViewRect.Width = X2 - X1;
mViewRect.Height = Y2 - Y1;
AdjustAspect();
}
private void SetBufferContext()
{
if (mGfxBuff != null)
{
mGfxBuff.dispose();
mGfxBuff = null;
}
// Retrieves the BufferedGraphicsContext for the
// current application domain.
mContext = BufferedGraphicsManager.Current;
// Sets the maximum size for the primary graphics buffer
mContext.MaximumBuffer = new Size(this.Width + 1,this.Height + 1);
// Allocates a graphics buffer the size of this control
mGfxBuff = mContext.Allocate(CreateGraphics(),new Rectangle(0,this.Width,this.Height));
mGfx = mGfxBuff.Graphics;
}
/// <summary>
/// Sets the matrix required to draw to the specified view
/// </summary>
private void SetViewMatrix()
{
if (float.IsInfinity(mViewRect.Width) |
float.IsInfinity(mViewRect.Height)) return;
if (mViewRect.Width == 0 | mViewRect.Height == 0) return;
//The ratio between the actual size of the screen and the size of the
graphics.
mScaletoReal = (mClientRect.Width / mGfx.DpiX) / mViewRect.Width;
if (first == 0)
{
mScale_stick = mScaletoReal;
first = 1;
}
mMtxDraw.Reset();
mMtxDraw.Scale(mScaletoReal,mScaletoReal);
mMtxDraw.Translate(-mViewportCenter.X,mViewportCenter.Y);
mMtxDraw.Translate((mViewRect.Width / 2f),(mViewRect.Height / 2f));
mMtxDraw.Scale(1,-1);
//Flip the Y
//The matrix for the triad is the same as the other geometry but
without the scale
mMtxWCS.Reset();
mMtxWCS.Multiply(mMtxDraw);
mMtxWCS.Scale(1 / mScaletoReal,1 / mScaletoReal);
mPixelF = ((1 / mGfx.DpiX) / mScaletoReal);
mBlipSize = (mPixelF * 3f);
SetFeedbackMatrix();
}
不确定这是否有帮助,但这是我试图使外部寄宿生适当扩大规模的方法:
foreach (clsdisplayList p in mdisplayLists)
{
if (!p.InView)
{
continue;
}
if (p.Rapid)
{
mCurPen.DashStyle = System.Drawing.drawing2d.DashStyle.Custom;
mCurPen.DashPattern = mRapidDashStyle;
}
else
{
mCurPen.DashStyle = System.Drawing.drawing2d.DashStyle.solid;
}
path.AddLines(p.Points);
mCurPen.Color = p.Color;
g.DrawLines(mCurPen,p.Points);
LineFixUp(ref p.Points);
foreach (PointF line in p.Points)
{
if (Math.Abs(line.Y) > points)
{
points = Math.Abs(line.Y);
}
if (Math.Abs(line.X) > points)
{
pointsx = Math.Abs(line.X);
}
try
{
if (line.Y > 0)
{
uSEOffsettranslation = true;
}
if (line.Y < ytop)
{
points = line.Y;
ytop = line.Y;
}
if (line.Y > ybottom)
{
ybottom = line.Y;
}
if (line.X < xleft)
{
xleft = line.X;
}
if (line.X > xright)
{
xright = line.X;
}
}
catch { }
}
}
path.Flatten();
mCurPen.Color = Color.Red;
float third = 0;
float fourth = 0;
try
{
float first = Math.Abs(xleft) * 2;
float second = (Math.Abs(xleft) * 2) + 2;
third = (second / first);
}
catch { }
try
{
float first = Math.Abs(ytop / 2) * 2;
float second = (Math.Abs(ytop / 2) * 2) + 2; //trying to add 2 inches to oversize
fourth = (second / first);
}
catch { }
g.DrawPath(mCurPen,path);
float tran = (points + 2) / points;
tran = Math.Abs(aspect - tran) + tran;
mCurPen.Color = Color.Pink;
float off = fourth * .85f;
Matrix translateMatrix = new Matrix();
translateMatrix.Scale((float)third,(float)fourth);
if (uSEOffsettranslation == false)// if G54 is not center
{
translateMatrix.Translate((float)0,off);
}
else
{
translateMatrix.Translate(0,0);
}
g.DrawPath(mCurPen,path);
文件的gcode也是:
G42 G01 Y-9.8484 G02 X-14.62 Y-7.8484 Z-0.0787 R2 G02 X-14.4063 Y-3.147 Z-0.0787 R51.8248 G02 X-11.441 Y-0.1132 Z-0.0787 R3.3602 G02 X-9.4488 Y0 Z-0.0787 R17.5728 G01 X5.9055 G02 X11.331 Y-0.1248 Z-0.0787 R117.9665 G02 X14.5344 Y-3.3534 Z-0.0787 R3.3603 G02 X14.6201 Y-7.8481 Z-0.0787 R117.9602 G02 X14.5332 Y-12.3735 Z-0.0787 R117.9602 G02 X9.9758 Y-17.7879 Z-0.0787 R5.7617 G02 X5.9054 Y-18.2165 Z-0.0787 R19.5415 G02 X1.8352 Y-17.7879 Z-0.0787 R19.5415 G02 X-1.0066 Y-16.2587 Z-0.0787 R5.7618 G02 X-1.0751 Y-16.1879 Z-0.0787 R2 G03 X-2.1946 Y-15.6968 Z-0.0787 R1.5217 G01 X-9.4488 G02 X-11.4409 Y-15.5835 Z-0.0787 R17.5728 G02 X-14.4064 Y-12.5498 Z-0.0787 R3.3602 G02 X-14.62 Y-7.8484 Z-0.0787 R51.8248 G02 X-12.62 Y-5.8484 Z-0.0787 R2 G40
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)