Author Topic: paletteset icon  (Read 2918 times)

0 Members and 1 Guest are viewing this topic.

nekitip

  • Guest
paletteset icon
« on: August 07, 2014, 03:42:26 PM »
I'm having problems setting icon for a paletteset.
Because "Icon" as a type is not found. Namespace problem?
What I'm actually trying to do is to load icon in WPF from resources (once i figure out what is wrong with Icon type)

If helps - http://forums.autodesk.com/autodesk/attachments/autodesk/152/26712/1/CP205-2_Mike_Tuersley.pdf

nekitip

  • Guest
Re: paletteset icon
« Reply #1 on: August 07, 2014, 05:16:30 PM »
Some things i don't understand (actually, most of them but...)
I've had
Code - vb.net: [Select]
  1. Imports System.Drawing
and was sure that this is for importing namespaced types (to be able to instantiate classes...)
And now I've for fun added reference in reference manager for "system.drawings" and "icon" type has showed up. Still not tested if that is what I need in this palleteset case, but I'm just curious - what "imports system.drawings" or "using" in C# is for than, and why didn't Visual Studio cried error in this case?

nekitip

  • Guest
Re: paletteset icon
« Reply #2 on: August 10, 2014, 05:48:08 AM »
So for whoever googles to here, to be able to use your own icon in paletteset, you must include system.drawings in your visual studio references (and later import system.drawing) and then, somewhere in code:
Code - vb.net: [Select]
  1. Dim path = System.IO.Path.GetDirectoryName(New System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)
  2. 'path is in this case to your app starting location
  3. Dim bmpis As New System.Drawing.Bitmap(path & "\icon_32.png")
  4. Dim iconPNG As System.Drawing.Icon = System.Drawing.Icon.FromHandle(bmpis.GetHicon)
  5. yourpaletteset.Icon = iconPNG
  6.  
Now, the interesting thing to consider about WPF is that it allows to import "resources" images. Those are the images you can put in, lets say - resources folder. Set in properties of image  compiler flag to "resources", and then something like:
Code - vb.net: [Select]
  1. Dim iconUri As Uri = New Uri("pack://application:,,,/resources/icon_32.png")
  2. Dim imageInfo As Windows.Resources.StreamResourceInfo = System.Windows.Application.GetResourceStream(iconUri)
  3. '...= imageInfo.Stream
  4.  
However, this i cannot force to run, and my suspicion is that in Uri, maybe assembly should also be stated.  It all seems pretty strait forward, but it may be that Autocad makes it little tricky. But I bet it has to do with assembly name (check MSDN docs)
I haven't got the time to explore this direction, but if someone knows this better, please fill in.

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: paletteset icon
« Reply #3 on: August 11, 2014, 02:37:02 PM »
Here is the MSDN page on Pack URIs

Did you get one of the two sets of code shown above to work?  The translation is difficult to follow so I'm not sure if you're aware that your doing two different things in your last code example.  The first one is trying to load an image from disk and the second is trying to find the image within the current Application.  My guess is you have the wrong path specified  in the Uri creation.  Make sure your icon, "icon_32.png", is located in a folder/namespace, "resources", in your assembly.  Or change the path given in the Uri creation.

« Last Edit: August 11, 2014, 02:47:37 PM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

nekitip

  • Guest
Re: paletteset icon
« Reply #4 on: August 12, 2014, 12:28:03 PM »
Yes, I've given two examples, first one that works, and loads file from disk, and the other one that must work, but is not.
First one is to help people like me who were without idea how to load at all, for first help, yet the second one is here as an idea, to expand by the ones who know better than me.

The second example must work in WPF enviroment by loading file that is in your resurces folder (you create folder, click add existing file (the one you have copied there, in this case icon_32.png), and in properties of the file, set "resources" (not Embeded, not compiled, or...)
so, in your WPF app you will see folder "resources" and inside file icon_32.png
Ok
And now you should be able to  load it by converting URI to stream. The first thing is to create URI, then convert it to stream, and then hopefully to system.drawings.icon. But, converting it to stream is not working by using the most simple URI
Code - vb.net: [Select]
  1. Dim iconUri As Uri = New Uri("pack://application:,,,/resources/icon_32.png")
. At least in my case, it may work in yours. I suspect one should use URI in form of pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml where assembly must by your .dll because maybe WPF is trying to find file in autocad, not in this assembly (not in your .net autocad add-on). However, as I have said, I stopped here, and... if anyone knows, it would be nice to know this and add this code to some repository of standard routines.
« Last Edit: August 12, 2014, 03:57:08 PM by nekitip »