Editable Image Region - Plugin Update

Editable Image Region - Plugin Update - Сообщения

#21 Опубликовано: 20.03.2016 06:26:22
Davide Carpi

Davide Carpi

1415 сообщений из 2872 понравились пользователям.

Группа: Moderator

Alex,

before introducing multiple images I would suggest you to improve the code of the plugin. As for what I can see, LoadImage() is everywhere, that seems to me a waste of performances (especially for svg, psd, dxf, pdf, dwg files, where you have to read a lot of data and initialize it at any call); you should identify when the file must be accessed (when it is loaded the first time, on some specific actions, on evaluation?), everything else can be addressed using an internal resource (storing imageEo and/or the original resource in an global object)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#22 Опубликовано: 20.03.2016 12:37:23
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Im trying to set the width / height pramaters in the scg file by hand
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">



This code does not work and some iteration of it should. Any ideas?



using (FileStream streamSVG = new FileStream(loadFromPath, FileMode.Open, FileAccess.ReadWrite))
                    {
                        
                        XmlDocument svgFile = new XmlDocument();
                        svgFile.Load(streamSVG);
                        XmlElement svg = (XmlElement)svgFile.SelectSingleNode("//svg";
                        //if (!(svg.HasAttribute("viewbox" || svg.HasAttribute("viewBox" || svg.HasAttribute("Viewbox" || svg.HasAttribute("ViewBox"))
                        //{
                            svg.SetAttribute("width", "100%"; // Set to new value.
                            svg.SetAttribute("height", "100%"; // Set to new value.
                        //} 
                        svgFile.Save(streamSVG);
                    }

1 пользователям понравился этот пост
Davide Carpi 20.03.2016 19:21:00
#23 Опубликовано: 20.03.2016 12:48:23
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Wrote

Alex - Do you have an example(s) of how you typically use this plugin?

Thanks!



Use it all the time for:

1. Visually confirming and documenting correctness of excel_IO input output
2. Displaying part of excel sheet in SMath

For 1 and 2 you would benefit from "counter variable" formula kindly provided by Davide (to automate naming of excel screenshots)
http://en.smath.info/forum/yaf_postst7309_How-to-implement-counter-variable---function.aspx

3. Sketching. This is big. PDF is very good choice for this; here is workflow, you will need Bluebeam and Inkscape:

Method 1 (I use it 70% of the time):
- take a PDF from CAD team for further markup / sketching
- Bluebeam screen capture feature capures vector graphics (surprise!)
- insert image region, set its size on the page, select extension "PDF"
- double click to be opened in Bluebeam
- Insert copied vector graphics
- Markup, or save and open in Inkscape to edit vector graphics directly

Method 2 :

- insert image region, set its size on the page, select extension "PDF"
- double click
- markup in bluebeam - there is enough basic sketching tools for you not to need anything else

There are also supported CAD formats which you can use if you want..
#24 Опубликовано: 20.03.2016 12:53:41
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Wrote

... max size to upload 5.859 MB
the file of this peanut 4.19MB

REFUSED !

Here is that peanut

Tea Pot.gif




Hi Jean, allow me to be frank here.

It is very clear that image region is not useful much to you. Myself, and at least a couple of other people use it all the time at work, i.e. it is not "dead in the water".

I would not benefit much from overlaying a tiny teapot over a tiny persian carpet. Unfortunately use of large files is limited by the speed of matrix operations in SMath when you have 10's of the in one document, and I do.

Can we just leave it at that? This plugin is useful for some but not all..
1 пользователям понравился этот пост
frapuano 20.03.2016 12:56:00
#25 Опубликовано: 20.03.2016 12:58:25
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Wrote

Alex,

before introducing multiple images I would suggest you to improve the code of the plugin. As for what I can see, LoadImage() is everywhere, that seems to me a waste of performances (especially for svg, psd, dxf, pdf, dwg files, where you have to read a lot of data and initialize it at any call); you should identify when the file must be accessed (when it is loaded the first time, on some specific actions, on evaluation?), everything else can be addressed using an internal resource (storing imageEo and/or the original resource in an global object)



I agree that some cleanup could be useful.. To be fair though, once LoadImage() is called, it will not reload it again till the image was changed (there is a Boolean flag for that).

So abundance of LoadImage() calls does not slow down the worksheet after initial loading.

1 пользователям понравился этот пост
Davide Carpi 20.03.2016 19:20:00
#26 Опубликовано: 20.03.2016 17:52:49
Ryan Freund

Ryan Freund

19 сообщений из 339 понравились пользователям.

Группа: User

Alex,

Thanks for the explaining that. I can see this being useful for including sketches in our calculation reports.

I can't remember - do you do structural engineering?

However I'm trying to understand the difference here - I could just make a sketch then take a screen shot and paste it into the calculation. It seems that with your plugin the sketch would be permanently linked to the smath sheet. So if I opened up the pdf with some editing software, made changes or added markups, this would then update in Smath. This is the general benefit, right?

Thanks again!
#27 Опубликовано: 20.03.2016 18:02:11
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Wrote

Alex,

Thanks for the explaining that. I can see this being useful for including sketches in our calculation reports.

I can't remember - do you do structural engineering?

However I'm trying to understand the difference here - I could just make a sketch then take a screen shot and paste it into the calculation. It seems that with your plugin the sketch would be permanently linked to the smath sheet. So if I opened up the pdf with some editing software, made changes or added markups, this would then update in Smath. This is the general benefit, right?

Thanks again!



That's the idea, the sketch file is saved within SMath workbook so you can edit it in future by double clicking. Works great for making changes to sketches you created.

You could of course take a new screenshot every time you make an edit. And keep track of the sketch files your are inserting into workbook.
#28 Опубликовано: 26.03.2016 16:37:05
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Still working on DISLIN botched SVG file, need to edit it to workable form.

I'm trying to set the width / height pramaters in the scg file by hand
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">[/code][/quote]

This code does not work and some iteration of it should. Any ideas?

[code=csharp]

using (FileStream streamSVG = new FileStream(loadFromPath, FileMode.Open, FileAccess.ReadWrite))
                    {
                        
                        XmlDocument svgFile = new XmlDocument();
                        svgFile.Load(streamSVG);
                        XmlElement svg = (XmlElement)svgFile.SelectSingleNode("//svg";
                        //if (!(svg.HasAttribute("viewbox" || svg.HasAttribute("viewBox" || svg.HasAttribute("Viewbox" || svg.HasAttribute("ViewBox"))
                        //{
                            svg.SetAttribute("width", "100%"; // Set to new value.
                            svg.SetAttribute("height", "100%"; // Set to new value.
                        //} 
                        svgFile.Save(streamSVG);
                    }


The exception is caught @ "setattribute"
"Object reference not set to an instance of an object."
#29 Опубликовано: 27.03.2016 13:05:54
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

This code successfully sets width & height to 100% if there is no viewbox (to fix the DISLIN svg) for ALL svg files but the one generated by DISLIN. It seems that the code hangs (literally) when trying to load DISLIN svg as a XML document. All other .svg files are loaded as XML document OK...



if (Path.GetExtension(loadFromPath).Equals(".svg" || Path.GetExtension(loadFromPath).Equals(".svgz")
            {
                try
                {
                    using (FileStream streamSVG = new FileStream(loadFromPath, FileMode.Open, FileAccess.ReadWrite))
                    {

                        XmlDocument svgFile = new XmlDocument();
                        svgFile.Load(streamSVG);
                        streamSVG.SetLength(0);
                        //XmlElement svg = (XmlElement)svgFile.SelectSingleNode("//svg";
                        XmlElement svg = svgFile.DocumentElement;
                        if (!(svg.HasAttribute("viewbox" || svg.HasAttribute("viewBox" || svg.HasAttribute("Viewbox" || svg.HasAttribute("ViewBox"))
                        {
                            svg.SetAttribute("width", "100%"; // Set to new value.
                            svg.SetAttribute("height", "100%"; // Set to new value.
                        }


                        //svgFile.DocumentElement.SetAttribute("width", "100%";
                        //svgFile.DocumentElement.SetAttribute("height", "100%";

                        svgFile.Save(streamSVG);

                        streamSVG.Close();
                    }
                    var SVGdoc = SvgDocument.Open(loadFromPath);
                    if (!(SVGdoc.OwnerDocument.ViewBox.Width == 0 || SVGdoc.OwnerDocument.ViewBox.Height == 0))
                    {
                        //SVGdoc.OwnerDocument.ViewBox = new SvgViewBox(0, 0, SVGdoc.Width, SVGdoc.Height);
                        // SVGdoc.ViewBox = new SvgViewBox(0, 0, baseWidth, baseHeight);
                        if (select_rotation == "90°" || select_rotation == "270°"
                        {
                            SVGdoc.Height = Size.Width;
                            SVGdoc.Width = Size.Height;
                        }
                        else
                        {
                            SVGdoc.Height = Size.Height;
                            SVGdoc.Width = Size.Width;
                        }
                        OLDheight = Convert.ToInt32(SVGdoc.OwnerDocument.ViewBox.Height);
                        OLDwidht = Convert.ToInt32(SVGdoc.OwnerDocument.ViewBox.Width);
                    }
                   
                    using (MemoryStream stream = new MemoryStream())
                    {
                        SVGdoc.Draw().Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        loadedImage = System.Drawing.Image.FromStream(stream);
                        imageWasChanged = false;
                        imageXml = System.Drawing.Image.FromStream(stream);
                    }
                    rotateImage(loadedImage, select_rotation);
                }
                catch
                {
                }
            }
#30 Опубликовано: 27.03.2016 14:20:41
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

I figured out why I cannot load DISLIN generated .svg as an XML document. Its the presence of this within the .svg:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
What is the quickest way to remove it from file before loading it as XML?

Once done, I could complete the fix for DISLIN .svg files.

Note that resulting svg file will be zoomable within SMath, but reset to original size will not work since the actual size of the plot is not present within .svg by DISLIN
#31 Опубликовано: 28.03.2016 19:06:09
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

If I modify / remove the offending line by hand (edit the .svg file) it works. If I do it using the following code, it seems like it works but it doesn't... (xml reader cannot file the root element )



if (Path.GetExtension(loadFromPath).Equals(".svg" || Path.GetExtension(loadFromPath).Equals(".svgz")
            {
                try
                {
                    //...modify svg file if necessary
                    using (FileStream streamSVG = new FileStream(loadFromPath, FileMode.Open, FileAccess.ReadWrite))
                    {
                        //...change the string in a file...not used
                        StreamReader readerSVG = new StreamReader(streamSVG);
                        string stringSVG = readerSVG.ReadToEnd();
                        if (stringSVG.Contains("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"")
                            stringSVG = stringSVG.Replace("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\"", "";
                        if (stringSVG.Contains("\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">")
                            stringSVG = stringSVG.Replace("\"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">", "";
                        byte[] arraySVG = Encoding.ASCII.GetBytes(stringSVG);
                        streamSVG.SetLength(0);
                        streamSVG.Write(arraySVG, 0, arraySVG.Length); 
                        //...done...change the string in a file...not used
                        //...edit XML if necessary
                        XmlDocument svgFile = new XmlDocument();
                        svgFile.Load(streamSVG);
                        streamSVG.SetLength(0);
                        //-this line was not used- did not work - XmlElement svg = (XmlElement)svgFile.SelectSingleNode("//svg";
                        XmlElement svg = svgFile.DocumentElement;
                        if (!(svg.HasAttribute("viewbox" || svg.HasAttribute("viewBox" || svg.HasAttribute("Viewbox" || svg.HasAttribute("ViewBox"))
                        {
                            double d;
                            if (double.TryParse(svg.GetAttribute("width", out d) && double.TryParse(svg.GetAttribute("height", out d))
                            {
                                OLDheight = Convert.ToInt32(svg.GetAttribute("height");
                                OLDwidht = Convert.ToInt32(svg.GetAttribute("width");
                            }                            
                            svg.SetAttribute("width", "100%"; // Set to new value.
                            svg.SetAttribute("height", "100%"; // Set to new value.
                        }
                        //...done...edit XML if necessary

                        svgFile.Save(streamSVG);

                        streamSVG.Close();
                    }
                    //...done...modify svg file if necessary
                    var SVGdoc = SvgDocument.Open(loadFromPath);
                    if (SVGdoc.OwnerDocument.ViewBox.Width == 0 || SVGdoc.OwnerDocument.ViewBox.Height == 0)
                    {
                        SVGdoc.OwnerDocument.ViewBox = new SvgViewBox(0, 0, Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Width), Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Height));
                        SVGdoc.Height = Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Height);
                        SVGdoc.Width = Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Width);
                    }
                    if (select_rotation == "90°" || select_rotation == "270°"
                    {
                        SVGdoc.Height = Size.Width;
                        SVGdoc.Width = Size.Height;
                    }
                    else
                    {
                        SVGdoc.Height = Size.Height;
                        SVGdoc.Width = Size.Width;
                    }
                    OLDheight = Convert.ToInt32(SVGdoc.OwnerDocument.ViewBox.Height);
                    OLDwidht = Convert.ToInt32(SVGdoc.OwnerDocument.ViewBox.Width);
                    using (MemoryStream stream = new MemoryStream())
                    {
                        SVGdoc.Draw().Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        loadedImage = System.Drawing.Image.FromStream(stream);
                        imageWasChanged = false;
                        imageXml = System.Drawing.Image.FromStream(stream);
                    }
                    rotateImage(loadedImage, select_rotation);                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

#32 Опубликовано: 28.03.2016 19:58:29
Davide Carpi

Davide Carpi

1415 сообщений из 2872 понравились пользователям.

Группа: Moderator

Wrote

I figured out why I cannot load DISLIN generated .svg as an XML document. Its the presence of this within the .svg:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
What is the quickest way to remove it from file before loading it as XML?

Once done, I could complete the fix for DISLIN .svg files.

Note that resulting svg file will be zoomable within SMath, but reset to original size will not work since the actual size of the plot is not present within .svg by DISLIN


I may be wrong but I guess this is because XmlDocument attempts to download the DTD

Instead of edit/removing the DOCTYPE, you can try using:

XmlDocument svgFile = new XmlDocument();
svgFile.XmlResolver = null;
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#33 Опубликовано: 29.03.2016 00:23:36
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

After many-many attempts (I have literally spent good 6 hours to fix display of DISLIN svg) I have found the "correct" way of fixing the missing viewport parameter.

Plugin was updated.



if (SVGdoc.OwnerDocument.ViewBox.Width == 0 || SVGdoc.OwnerDocument.ViewBox.Height == 0)
                    {
                        SVGdoc.Height = 1;
                        SVGdoc.Width = 1;
                        SVGdoc.Height = SVGdoc.Height.ToPercentage();
                        SVGdoc.Width = SVGdoc.Width.ToPercentage();

                        SVGdoc.OwnerDocument.ViewBox = new SvgViewBox(SVGdoc.OwnerDocument.Bounds.Left, SVGdoc.OwnerDocument.Bounds.Top, Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Width), Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Height));
                        SVGdoc.Height = Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Height);
                        SVGdoc.Width = Convert.ToInt32(SVGdoc.OwnerDocument.GetDimensions().Width);
                    }

Seems like an extremely simple piece of code now... Doesn't it always.
2 пользователям понравился этот пост
Вячеслав Мезенцев 29.03.2016 01:09:00, frapuano 29.03.2016 09:37:00
#34 Опубликовано: 29.03.2016 01:49:21
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Wrote

Alex,

before introducing multiple images I would suggest you to improve the code of the plugin. As for what I can see, LoadImage() is everywhere, that seems to me a waste of performances (especially for svg, psd, dxf, pdf, dwg files, where you have to read a lot of data and initialize it at any call); you should identify when the file must be accessed (when it is loaded the first time, on some specific actions, on evaluation?), everything else can be addressed using an internal resource (storing imageEo and/or the original resource in an global object)



Davide, I made an attempt to clean up function calls.

Would love to hear your comments / suggestions if any.

Thanks
#35 Опубликовано: 17.04.2016 01:20:04
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Please excuse me if this has been discussed already.
Is it possible to extend the plugin to accept matrices of 3(4)-numbers matrices (vectors/rows), where members are R, G, B (and optional Alpha)? And maybe functions to read to this structure / write it to image?

Or alternatively, a system of 3(4) usual matrices with corresponding values?
С уважением, Михаил Каганский
#36 Опубликовано: 17.04.2016 02:22:05
Alexander O. Melnik

Alexander O. Melnik

127 сообщений из 494 понравились пользователям.

Группа: Moderator

Wrote

Please excuse me if this has been discussed already.
Is it possible to extend the plugin to accept matrices of 3(4)-numbers matrices (vectors/rows), where members are R, G, B (and optional Alpha)? And maybe functions to read to this structure / write it to image?

Or alternatively, a system of 3(4) usual matrices with corresponding values?



Mike, I am somewhat busy this weekend, but I will look into this mid week or next weekend. It would greatly simplify my task if you were to find a C# guide that talks about converting numerical matrices to color image.

Alex
1 пользователям понравился этот пост
Mike Kaganski 17.04.2016 03:02:00
#37 Опубликовано: 17.04.2016 03:01:59
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Wrote

Mike, I am somewhat busy this weekend, but I will look into this mid week or next weekend. It would greatly simplify my task if you were to find a C# guide that talks about converting numerical matrices to color image.



Thank you very much!
I'm not quite sure I understand what information do you mean.
I suppose that the place to change is lines starting from 349 in ImageRegion.cs (public method void ImageRegion.OnEvaluation(SMath.Math.Store store) - where it processes matrix). If you will support systems, then third choice would likely be required - "else if (obj is SMath.Math.Numeric.TSystem)" (I suppose the name is TSystem).

Lines 381-392 assign same (gray) values to R, G and B channels. I suppose, here the current mode should be kept if our matrix contains numbers, otherwise if its contents in this position is another matrix of length 3 (4), then assign R, G and B to individual channels. To do so, line 383 should change from unconditionally getting byte value, to a code that checks what is there at matrix.unit[y, x].obj, and if it is SMath.Math.Numeric.TMatrix, then extract its 3 members, otherwise go old way.

Here I see a method to set alpha channel; maybe it's good to always use alpha channel, and set it always to 0 unless it's present in the pixel?

EDIT: generating and manipulation separate R, G, B and Alpha matrices is much more efficient than matrices of matrices, so if you decide to implement this request, then please allow second variant, too (I mean system of 3(4) separate matrices).
С уважением, Михаил Каганский
#38 Опубликовано: 17.04.2016 11:07:11
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Wrote

Please excuse me if this has been discussed already.
Is it possible to extend the plugin to accept matrices of 3(4)-numbers matrices (vectors/rows), where members are R, G, B (and optional Alpha)? And maybe functions to read to this structure / write it to image?



In other words, you are asking for an RGB image region, something like the Mathcad
"Picture Operator". I found no independent tool from the web that would do same.
The Mthcad "PicOp" allows lot of useful manipulations: various color scheme, extract selected region from the image... Are you familiar with the Mathcad "PicOp" ?

That's all what I understand from Smath 5346

Forum Image COLOR.gif

#39 Опубликовано: 17.04.2016 11:20:01
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Yes, Jean, I was asking for this And exactly because of your Ventouri rotation task - I wanted to see it rotated coloured.
С уважением, Михаил Каганский
#40 Опубликовано: 17.04.2016 11:23:55
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

In short: Smath Image region does not read color image, it "Imports" [nuance].
Once in work sheet, "Image region" does 0 processing by itself.
From external user processing, you can:
1. replace part of image
2. zoom Fourier small image [zoom up 2]
3. vetorise for light intensity.
4. mirror image, rotate +/- 90° by matrix manipulation
5. scale overal image to user grayscale.
6. what else ... extract band valued pixels, specific valued [locate]
7. more specific vetorise to highlight contour region.
..... Jean
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений