Page Index Toggle Pages: [1] 2  Send TopicPrint
Very Hot Topic (More than 25 Replies) Change the measurement unit in Diagram (Read 6614 times)
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Change the measurement unit in Diagram
Mar 30th, 2020 at 7:11am
Print Post  
SmileyI want to change the measurement unit of the diagram to millimeters. How do I set the properties of Diagram.MeasureUnit and Ruler.Unit? Is there any sample code?
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #1 - Mar 30th, 2020 at 7:37am
Print Post  
After changing the measurement unit, do the following code need to be changed accordingly?

            var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
var imageWidth = GraphicsUnit.Pixel.Convert(imageWidthPx, diagram.MeasureUnit);
var imageHeight = GraphicsUnit.Pixel.Convert(imageHeightPx, diagram.MeasureUnit);
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #2 - Mar 30th, 2020 at 9:47am
Print Post  
Try this:

Code
Select All
diagram.MeasureUnit = GraphicsUnit.Millimeter;
ruler.Unit = RulerUnit.Millimeter; 



Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #3 - Mar 30th, 2020 at 11:08am
Print Post  
Thanks a lot, Lyubo. Smiley

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            diagram.MeasureUnit = GraphicsUnit.Millimeter;
            ruler.Unit = RulerUnit.Millimeter;

        }

private void ImageManipulation(string path)
        {
            var myImage = new BitmapImage(new Uri(path));

            var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
            var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
            var imageWidth = GraphicsUnit.Pixel.Convert(imageWidthPx, diagram.MeasureUnit);
            var imageHeight = GraphicsUnit.Pixel.Convert(imageHeightPx, diagram.MeasureUnit);

            imageNode = diagram.Factory.CreateShapeNode(0, 0, imageWidth, imageHeight);
            imageNode.Image = myImage;
            imageNode.Shape = Shapes.Rectangle;
            imageNode.CustomDraw = CustomDraw.Additional;
            imageNode.ImageAlign = ImageAlign.Fit;

            var overlayNodeWidth = 200;
            var overlayNodeHeight = 200;

            overlayNode = diagram.Factory.CreateShapeNode(imageNode.Bounds.Right - overlayNodeWidth,
  imageNode.Bounds.Bottom - overlayNodeHeight, overlayNodeWidth, overlayNodeHeight);
            overlayNode.Shape = Shapes.Rectangle;
            overlayNode.Brush = Brushes.Transparent;
            overlayNode.Locked = true;
            overlayNode.AttachTo(imageNode, AttachToNode.BottomRight);
        }

I used the above code, but the length and width of the displayed imageNode differs from the actual length and width by about 35 mm. Does this code need to be adjusted: var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
            var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
            var imageWidth = GraphicsUnit.Pixel.Convert (imageWidthPx, diagram.MeasureUnit);
            var imageHeight = GraphicsUnit.Pixel.Convert (imageHeightPx, diagram.MeasureUnit);
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #4 - Mar 30th, 2020 at 11:18am
Print Post  
Added:
This is what I currently do with the above code.
  

3_30_9.png ( 1250 KB | 123 Downloads )
3_30_9.png
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #5 - Mar 30th, 2020 at 11:19am
Print Post  
Added:
This is the actual size.
  

3_30_8.png ( 1551 KB | 121 Downloads )
3_30_8.png
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #6 - Mar 30th, 2020 at 11:26am
Print Post  
What is the size of your image file and at what point do you call that ImageManipulation method? Before or after changing the measure unit? The conversion code doesn't need to change, as it checks the currently provided value for the diagram's measure unit, so if it's in millimeters when you check it, it should return a correct value.
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #7 - Mar 30th, 2020 at 1:12pm
Print Post  
Hey, Lyubo. Smiley
The picture size is shown in Figure 1: 2439X1322.
When I open the local image successfully, I call the ImageManipulation method. But the result is different from the actual one. I measured the real circuit board with a real tape measure and it is indeed a small value measured by our Diagram. Don't know where the problem is? What changes should be made?
I urgently need to resolve this issue to continue working. Smiley
  

3_30_11.png ( 131 KB | 126 Downloads )
3_30_11.png
3_30_12.png ( 235 KB | 141 Downloads )
3_30_12.png
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #8 - Mar 30th, 2020 at 1:27pm
Print Post  
Hi,

The code works as expected in my test with an image with the same specifications as yours - 2439x1322px 254dpi. Debug your code at the point of creating the node and check if the value of diagram.MeasureUnit is actually GraphicsUnit.Millimeter. Check also if you're not changing the bounds of the node somewhere else.

Regards,
Lyubo
MindFusion
  

imagemm.png ( 3 KB | 118 Downloads )
imagemm.png
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #9 - Mar 30th, 2020 at 1:48pm
Print Post  
Hey, Lyubo. Smiley
I have monitored the following variables step by step. I set the node size of imageNode at this location. There must be something wrong with this ImageManipulation (string path) method. Help me see what changes I need to make in this code.
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #10 - Mar 30th, 2020 at 1:54pm
Print Post  
Added:
Pictures of my step-by-step debugging Smiley
  

_____________20200330220208.png ( 252 KB | 132 Downloads )
_____________20200330220208.png
_____________20200330220219.png ( 212 KB | 132 Downloads )
_____________20200330220219.png
_____________20200330220230.png ( 208 KB | 130 Downloads )
_____________20200330220230.png
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #11 - Mar 30th, 2020 at 2:22pm
Print Post  
Is something wrong here? because it involves two nodes.

private void ImageManipulation(string path)
        {
            var myImage = new BitmapImage(new Uri(path));

            var imageWidthPx = myImage.PixelWidth * 96 / myImage.DpiX;
            var imageHeightPx = myImage.PixelHeight * 96 / myImage.DpiY;
            var imageWidth = GraphicsUnit.Pixel.Convert(imageWidthPx, diagram.MeasureUnit);
            var imageHeight = GraphicsUnit.Pixel.Convert(imageHeightPx, diagram.MeasureUnit);

            imageNode = diagram.Factory.CreateShapeNode(0, 0, imageWidth, imageHeight);
            imageNode.Image = myImage;
            imageNode.Shape = Shapes.Rectangle;
            imageNode.CustomDraw = CustomDraw.Additional;
            imageNode.ImageAlign = ImageAlign.Fit;

            //overlayNode = diagram.Factory.CreateShapeNode(0, 0, 150, 150);
            var overlayNodeWidth = 50;
            var overlayNodeHeight = 50;

            overlayNode = diagram.Factory.CreateShapeNode(imageNode.Bounds.Right - overlayNodeWidth,
  imageNode.Bounds.Bottom - overlayNodeHeight, overlayNodeWidth, overlayNodeHeight);
            overlayNode.Shape = Shapes.Rectangle;
            overlayNode.Brush = Brushes.Transparent;
            //overlayNode.StrokeThickness = 0;
            overlayNode.Locked = true;
            overlayNode.AttachTo(imageNode, AttachToNode.BottomRight);
        }

private void diagram_DrawNode(object sender, DrawNodeEventArgs e)
{
if (e.Node != imageNode)
return;

var graphics = e.Graphics;
var clip = new CombinedGeometry(
GeometryCombineMode.Exclude,
new RectangleGeometry(imageNode.Bounds),
new RectangleGeometry(overlayNode.Bounds));
var translate = new TranslateTransform(-imageNode.Bounds.X, -imageNode.Bounds.Y);
graphics.PushTransform(translate);
graphics.PushClip(clip);
graphics.DrawRectangle(new SolidColorBrush(Color.FromArgb(120, 0, 0, 0)), null, imageNode.Bounds);
graphics.Pop();
graphics.Pop();
}
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #12 - Mar 30th, 2020 at 2:30pm
Print Post  
Hi,

The values for imageWidthPx and imageHeightPx are exactly when they need to be. Width of 2439px in 254dpi, gives width of 921.8px in 96dpi (which the conversion code expects). Same for the height - the value is correct.

An error you're doing is actually tracking the node's ActualWidth and ActualHeight properties, which do not represent the the node dimensions in millimeters. As already pointed out several times, use the node Bounds property for all location/size manipulations. The values for imageNode.Bounds.Width and imageNode.Bounds.Height should be 243.9 and 132.2 respectively (values are in millimeters this time) - which is exactly the image size converted.

As advised before, make sure that the diagram.MeasureUnit and ruler.Unit values are actually in mm, and that they aren't changed at a later time. Same goes for the imageNode.Bounds.

Regards,
Lyubo
MindFusion
  
Back to top
 
IP Logged
 
D
Full Member
***
Offline


I Love MindFusion!

Posts: 158
Joined: Mar 16th, 2020
Re: Change the measurement unit in Diagram
Reply #13 - Mar 30th, 2020 at 2:51pm
Print Post  
Both imageWidthPx and imageHeightPx are correct values. There are errors in the imageWidth and imageHeight converted from the GraphicsUnit.Pixel.Convert method, respectively. Then it proves that there is an error in the GraphicsUnit.Pixel.Convert method. Please tell me how to change it.
  

3_41.png ( 281 KB | 129 Downloads )
3_41.png
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: Change the measurement unit in Diagram
Reply #14 - Mar 30th, 2020 at 3:12pm
Print Post  
If you're getting these values for imageWidth and imageHeigh, most likely the graphics.MeasureUnit is not set to Millimeter. As asked before, make sure that it's in fact at the correct setting.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send TopicPrint