Fixed logic to properly position XY Plots and Images

This commit is contained in:
Rich 2017-01-25 18:41:56 +00:00
parent cdb7521b01
commit e6c65b5139

View File

@ -752,6 +752,7 @@ namespace VEPROMS.CSLA.Library
int filecount = 1; int filecount = 1;
while (selxy != null) while (selxy != null)
{ {
// B2017-007 Fixed logic to properly position an XY Plot
string xyplot = selxy.Text; string xyplot = selxy.Text;
xyplot = xyplot.Replace("`", "\xB0"); xyplot = xyplot.Replace("`", "\xB0");
xyplot = xyplot.Replace("\xF8", "\xB0"); xyplot = xyplot.Replace("\xF8", "\xB0");
@ -771,17 +772,8 @@ namespace VEPROMS.CSLA.Library
// Setting the selected text to "" actually sets it to "\r", thus cnt-- (subtract one // Setting the selected text to "" actually sets it to "\r", thus cnt-- (subtract one
// from cnt since there's already a return in the string. // from cnt since there's already a return in the string.
cnt--; cnt--;
float x = adjustMargins ? (float)selxy.get_Information(LBWdInformation.wdHorizontalPositionRelativeToTextBoundary) : PointF pt = GetLocation(selxy,adjustMargins);
(float)selxy.get_Information(LBWdInformation.wdHorizontalPositionRelativeToPage); float y = pt.Y;
float y = adjustMargins ? (float)selxy.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary) :
(float)selxy.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage);
LBRange myRange = selxy.Paragraphs.First.Range;
float yTop = adjustMargins ?(float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary) :
(float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage);
//float yTop1 = (float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary);
// some data had regular text embedded in the xyplot definition. If so, the plain text must
// be left in the file. Otherwise, we can replace with empty text.
string resXyPlot = xyplot; string resXyPlot = xyplot;
string txt = FindEmbeddedText(selxy.Text, ref resXyPlot); string txt = FindEmbeddedText(selxy.Text, ref resXyPlot);
if (txt != null) if (txt != null)
@ -796,17 +788,30 @@ namespace VEPROMS.CSLA.Library
} }
pngFile = VlnSettings.TemporaryFolder + @"\XYPlot" + filecount.ToString() + @".png"; //@"C:\Temp\XYPlot1.png"; pngFile = VlnSettings.TemporaryFolder + @"\XYPlot" + filecount.ToString() + @".png"; //@"C:\Temp\XYPlot1.png";
//FileInfo fi = new FileInfo(pngFile);
//_MyLog.WarnFormat("PNG Name = {0}, size = {1}", fi.Name, fi.Length);
filecount++; filecount++;
RectangleF plotRect = CreatePlot(pngFile, xyplot, 600F, FormForPlotGraphics);
float xAdjust = 0; //(float)-sect.MyDocStyle.Layout.LeftMargin; RectangleF plotRect = CreatePlot(pngFile, xyplot, 600F, FormForPlotGraphics);
float yAdjust = selxy.Font.Size; float yAdjust = selxy.Font.Size;
LBShape shape = myDoc.Shapes.AddPicture(pngFile, x + xAdjust + plotRect.X, yAdjust + y - yTop + plotRect.Y, selxy.Range); float xxx = pt.X + plotRect.X;
if (xxx < 0 && xxx > -.5)
xxx = 0;
float yyy = yAdjust + y + plotRect.Y;
LBShape shape = myDoc.Shapes.AddPicture(pngFile, xxx, yyy, selxy.Range);
File.Delete(pngFile); File.Delete(pngFile);
if (adjustMargins)
{
shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;// .wdRelativeHorizontalPositionMargin;
}
else
{
shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;// .wdRelativeHorizontalPositionMargin;
}
shape.LockAspectRatio = LBMsoTriState.msoTrue; shape.LockAspectRatio = LBMsoTriState.msoTrue;
shape.Width = plotRect.Width; shape.Width = plotRect.Width;
shape.Left = xxx;
shape.Top = pt.Y;
selxy.WholeStory(); selxy.WholeStory();
selxy = FindXyPlot(); selxy = FindXyPlot();
} }
@ -865,14 +870,14 @@ namespace VEPROMS.CSLA.Library
float width = 72 * Int32.Parse(vals[3], System.Globalization.NumberStyles.AllowHexSpecifier) / 12.0F; float width = 72 * Int32.Parse(vals[3], System.Globalization.NumberStyles.AllowHexSpecifier) / 12.0F;
int lines = Int32.Parse(vals[2], System.Globalization.NumberStyles.AllowHexSpecifier); int lines = Int32.Parse(vals[2], System.Globalization.NumberStyles.AllowHexSpecifier);
float height = 72 * lines / 6.0F; float height = 72 * lines / 6.0F;
float x = adjustMargins ? (float)sel.get_Information(LBWdInformation.wdHorizontalPositionRelativeToTextBoundary): PointF pt = GetLocation(sel, adjustMargins);
(float)sel.get_Information(LBWdInformation.wdHorizontalPositionRelativeToPage); float xxx = pt.X;
// 9.25 is the approximate offset of the base line of the font, i.e. doesn't include descender. if (xxx < 0 && xxx > -.5)
float locvertpage = adjustMargins ? (float)sel.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary) : xxx = 0;
(float)sel.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage); float yyy = pt.Y;
float y = locvertpage + ((sel.Font.Size * 9.25F) / 12);
sel.Text = ""; sel.Text = "";
LBShape shape = myDoc.Shapes.AddPicture(roImageFile.MyFile.FullName, x, y, sel.Range); LBShape shape = myDoc.Shapes.AddPicture(roImageFile.MyFile.FullName, xxx, yyy, sel.Range);
File.Delete(pngFile);
if (adjustMargins) if (adjustMargins)
{ {
shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionMargin; shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
@ -883,10 +888,11 @@ namespace VEPROMS.CSLA.Library
shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionPage; shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;// .wdRelativeHorizontalPositionMargin; shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;// .wdRelativeHorizontalPositionMargin;
} }
shape.Top = y; // Reset value after setting flag RelativeVerticalPosition shape.LockAspectRatio = LBMsoTriState.msoTrue;
shape.Left = x;
shape.Width = width; shape.Width = width;
shape.Height = height; shape.Height = height;
shape.Left = xxx;
shape.Top = pt.Y;
imageROTokenReplaced = true; imageROTokenReplaced = true;
} }
if (!imageROTokenReplaced) if (!imageROTokenReplaced)
@ -894,6 +900,7 @@ namespace VEPROMS.CSLA.Library
} }
else if ((int)type == 4) // X-Y Plot else if ((int)type == 4) // X-Y Plot
{ {
// B2017-007 Fixed logic to properly position an XY Plot
try try
{ {
val = val.Replace("`", "\xB0"); val = val.Replace("`", "\xB0");
@ -905,40 +912,23 @@ namespace VEPROMS.CSLA.Library
int pstart = val.IndexOf("<<G"); // find the starting Plot Command int pstart = val.IndexOf("<<G"); // find the starting Plot Command
AddPrecedingText(sel, val.Substring(0, pstart),0.0F);// replace the RO token with what's in front of the X/Y Plot AddPrecedingText(sel, val.Substring(0, pstart),0.0F);// replace the RO token with what's in front of the X/Y Plot
val = val.Substring(pstart); // set val to the start of the plot commands val = val.Substring(pstart); // set val to the start of the plot commands
//float width = 72 * Int32.Parse(vals[3], System.Globalization.NumberStyles.AllowHexSpecifier) / 12.0F;
//float height = 72 * lines / 6.0F;
pngFile = VlnSettings.TemporaryFolder + @"\XYPlot1.png"; //@"C:\Temp\XYPlot1.png"; pngFile = VlnSettings.TemporaryFolder + @"\XYPlot1.png"; //@"C:\Temp\XYPlot1.png";
RectangleF plotRect = CreatePlot(pngFile, val, 600F, FormForPlotGraphics); RectangleF plotRect = CreatePlot(pngFile, val, 600F, FormForPlotGraphics);
//FileInfo fi = new FileInfo(pngFile); PointF pt = GetLocation(sel,adjustMargins);
//_MyLog.WarnFormat("PNG Name = {0}, size = {1}", fi.Name, fi.Length); float xxx = pt.X + plotRect.X;
if (xxx < 0 && xxx > -.5)
//LBShape shape = myDoc.Shapes.AddPicture(@"C:\Temp\XYPlot.png", 0, 0, sel.Range); xxx = 0;
float x = adjustMargins ? (float)sel.get_Information(LBWdInformation.wdHorizontalPositionRelativeToTextBoundary) : float y = pt.Y;
(float)sel.get_Information(LBWdInformation.wdHorizontalPositionRelativeToPage);
float y = adjustMargins ? (float)sel.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary):
(float)sel.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage);
// if there is regular text before the plot, then count the number of lines
// and adjust the y position accordingly
int idx = sel.Text.IndexOf("\r"); int idx = sel.Text.IndexOf("\r");
while ((idx > 0) && (idx < sel.Text.Length + 1)) while ((idx > 0) && (idx < sel.Text.Length + 1))
{ {
idx = sel.Text.IndexOf("\r", idx + 1); idx = sel.Text.IndexOf("\r", idx + 1);
y += (float)sel.ParagraphFormat.LineSpacing; // get_Information(LBWdLineSpacing.wdLineSpaceSingle); y += (float)sel.ParagraphFormat.LineSpacing; // get_Information(LBWdLineSpacing.wdLineSpaceSingle);
} }
//LBInlineShape shape = sel.InlineShapes.AddPicture(pngFile);
LBRange myRange = sel.Paragraphs.First.Range;
float yTop = adjustMargins ? (float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToTextBoundary) :
(float)myRange.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage);
float xAdjust = 0;// = (float)-sect.MyDocStyle.Layout.LeftMargin;
float yAdjust = sel.Font.Size; float yAdjust = sel.Font.Size;
// The following two lines made the xyplot location for WCN2 match the 16bit output. However, how the float yyy = yAdjust + y + plotRect.Y;
// xTweak value is determined is phantom of Rich's mind. Word document location of the RO token LBShape shape = myDoc.Shapes.AddPicture(pngFile, xxx, yyy, sel.Range);
// will be modified in data before delivery so that this xTweak is not needed.
//float xTweak = -(30 + xAdjust) / 3; // Used to align plots for WCN2.
//LBShape shape = myDoc.Shapes.AddPicture(pngFile, x + xAdjust + plotRect.X + xTweak, yAdjust + y - yTop + plotRect.Y, sel.Range);
LBShape shape = myDoc.Shapes.AddPicture(pngFile, x + xAdjust + plotRect.X, yAdjust + y - yTop + plotRect.Y, sel.Range);
File.Delete(pngFile); File.Delete(pngFile);
//shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;// .wdRelativeHorizontalPositionMargin;
if (adjustMargins) if (adjustMargins)
{ {
shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionMargin; shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
@ -949,20 +939,11 @@ namespace VEPROMS.CSLA.Library
shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionPage; shape.RelativeVerticalPosition = LBWdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;// .wdRelativeHorizontalPositionMargin; shape.RelativeHorizontalPosition = LBWdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;// .wdRelativeHorizontalPositionMargin;
} }
shape.Left = x + xAdjust + plotRect.X;
//shape.Top = yAdjust + y - yTop + plotRect.Y;
shape.Top = yAdjust + y + plotRect.Y;
shape.LockAspectRatio = LBMsoTriState.msoTrue; shape.LockAspectRatio = LBMsoTriState.msoTrue;
//shape.Width = .89F * shape.Width;
//shape.Width = float.Parse(tbAdjust.Text) * shape.Width;
shape.Width = plotRect.Width; shape.Width = plotRect.Width;
//shape.Height = .89F * shape.Height; shape.Left = xxx;
shape.Top = pt.Y;
sel.WholeStory(); sel.WholeStory();
// TODO: Do we want to color code ROs
//sel.Range.Font.Color = LBWdColor.wdColorRed;
//shape.Width = width;
//shape.Height = height;
//imageROTokenReplaced = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -1013,6 +994,18 @@ namespace VEPROMS.CSLA.Library
return fileName; return fileName;
} }
} }
private static PointF GetLocation(LBSelection sel, bool adjustMargins)
{
LBRange rng0 = MyApp.ActiveDocument.Range(0, 0);
LBRange rng1 = MyApp.ActiveDocument.Range(sel.Start, sel.Start);
float fx0 = adjustMargins ? (float)rng0.get_Information(LBWdInformation.wdHorizontalPositionRelativeToPage) : 0;
float fy0 = adjustMargins ? (float)rng0.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage) : 0;
float fx1 = (float)rng1.get_Information(LBWdInformation.wdHorizontalPositionRelativeToPage);
float fy1 = (float)rng1.get_Information(LBWdInformation.wdVerticalPositionRelativeToPage);
//_MyLog.WarnFormat("X {0}-{1}={2}",fx1, fx0, fx1-fx0);
//_MyLog.WarnFormat("Y {0}-{1}={2}",fy1, fy0, fy1-fy0);
return new PointF(fx1-fx0,fy1-fy0);
}
// Reads the Word file containing the resolved ROs // Reads the Word file containing the resolved ROs
// used when adding to DocReplace // used when adding to DocReplace
private static byte[] GetBytes(string filename) private static byte[] GetBytes(string filename)