Creating a stencil with images using a program

Started by yoursashok, April 08, 2010, 01:41:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

yoursashok

Hi,

Is there a way to create a Visio Stencil using a set of images in any of the program languages (preferably C#). I believe it should be possible, but could not find any examples yet. Anyone who had done this, please share the details or an example.

Thanks in advance.

Regards,
Ashok.

Yacine

Hi Ashok,
please apologize the naive answer, but I put some pictures in a drawing and recorded then this macro.

Sub Macro1()

    Application.Documents.AddEx "", visMSDefault, visAddStencil + visOpenDocked

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Drop On Stencil")
    ActiveWindow.DeselectAll
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect
    Dim vsoSelection1 As Visio.Selection
    Set vsoSelection1 = ActiveWindow.Selection
    Dim vsoDoc1 As Visio.Document
    Set vsoDoc1 = Application.Documents.Item("Stencil3")
    vsoDoc1.Drop vsoSelection1, 0#, 0#
    vsoSelection1.Delete
    Application.EndUndoScope UndoScopeID1, True

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Drop On Stencil")
    ActiveWindow.DeselectAll
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(2), visSelect
    Dim vsoSelection2 As Visio.Selection
    Set vsoSelection2 = ActiveWindow.Selection
    Dim vsoDoc2 As Visio.Document
    Set vsoDoc2 = Application.Documents.Item("Stencil3")
    vsoDoc2.Drop vsoSelection2, 0#, 0#
    vsoSelection2.Delete
    Application.EndUndoScope UndoScopeID2, True

    Application.Documents.Item("Stencil3").SaveAs "D:\...\Stencil3.vss"

End Sub


You would abviously start by defining a stencil ( Set vsoDoc1 = Application.Documents.Item("Stencil3")), then iterate through your shapes (for each shp in activewindow.shapes...) and drop each of the shapes on your stensil (vsoDoc1.drop shp) and you'll finish by saving as.
Yacine

yoursashok

Thanks Yacine. Will check the steps and let you know.

Regards,
Ashok.

yoursashok

Hi Yacine,

Do you have a C# sample (sorry am a new bie to this development). I couldn't understand what you are dropping in to the document. I need to include a set of images from my desktop and create a stencil with all those images in to it.

Regards,
Ashok.

Nikolay

Please take a look at this sample:


Sub ImportPictureAsMaster(docStencil, picturePath)
    Set newMaster = docStencil.Masters.Add
    newMaster.NameU = Mid(picturePath, InStrRev(picturePath, "\") + 1) ' <= set file name as master name
   
    Set newMasterCopy = newMaster.Open ' <= do master edit via master copy to auto-update icon
    newMasterCopy.Import picturePath
    newMasterCopy.Close
End Sub

Sub CreateNewStencil()
    Set docStencil = Application.Documents.AddEx("", , visAddStencil) ' <= in case you run external script, visAddStencil=512
   
    ImportPictureAsMaster docStencil, "C:\Users\Nikolay\Pictures\pic1.png"
    ImportPictureAsMaster docStencil, "C:\Users\Nikolay\Pictures\pic2.png"
   
    docStencil.SaveAs "C:\Users\Nikolay\Pictures\myStencil.vss"
    docStencil.Close
End Sub

Nikolay

#5
The above code rewritten in C# 4 (VS2010)
No PIA, no references to Visio at all, dynamic binding, named parameters

"Water, pure water with nothing more than a few drops of LSD in it!" (c) Dr. Hubert J. Farnsworth :D


using System;
using Microsoft.CSharp;
using System.IO;

namespace MyApp
{
   class Program
   {
       static void ImportPictureAsMaster(dynamic docStencil, string picturePath)
       {
           var newMaster = docStencil.Masters.Add();
           newMaster.NameU = Path.GetFileName(picturePath);
           
           var newMasterCopy = newMaster.Open();
           newMasterCopy.Import(picturePath);
           newMasterCopy.Close();
       }

       static void Main(string[] args)
       {
           dynamic app = Activator.CreateInstance(Type.GetTypeFromProgID("Visio.InvisibleApp"));
           var docStencil = app.Documents.AddEx("", Flags : 512);

           ImportPictureAsMaster(docStencil, @"C:\Users\bnk\Pictures\pic1.png");
           ImportPictureAsMaster(docStencil, @"C:\Users\bnk\Pictures\pic2.png");
           
           docStencil.SaveAs(@"C:\Users\bnk\Pictures\myStencil.vss");
           docStencil.Close();

           app.Quit();
       }
   }
}

yoursashok

Hi Nikolay,

Thanks much for the sample. I'm using Visual Studio 2008 and I believe it is not C# 4. So I replaced the dynamic with the static equivalents Master in this case. I got the stencil saved once, but after that, I always get an exception in the following line,

Master newMasterCopy = newMaster.Open();

The exception is,

Cannot create object.
   at Microsoft.Office.Interop.Visio.MasterClass.Open()
   at orStencilCreat.Class1.ImportPictureAsMaster(Document docStencil, String picturePath, String name) in C:\Documents and Settings\kashok\My Documents\Visual Studio 2008\Projects\orStencilCreat\orStencilCreat\Class1.cs:line 19

Not sure whether I'm missing something?

Many thanks for your help.

Regards,
Ashok.

Nikolay

#7
Okay, I just was in a process of "looking at" the C# 4 :)
Here is the code for "normal" C# - it seems to "work on my macnine"... I have checked it against Visio '03, '07 and '10.

Maybe you have Visio instance "hanging" in memory for some reason?
In this case, try killing all such Visio instances prior to running the code.

Please note, this code assumes you do have "pic1.png" and "pic2.png" picture files! Please replace these with YOUR pictures you want to add!
Also it assumes that folder "C:\Users\bnk\Pictures\" does exist! Replace it with a real one :D


using System;
using Microsoft.Office.Interop.Visio;

namespace MyApp
{
   class Program
   {
       static void ImportPictureAsMaster(IVDocument docStencil, string picturePath)
       {
           IVMaster newMaster = docStencil.Masters.Add();
           newMaster.NameU = System.IO.Path.GetFileName(picturePath);

           IVMaster newMasterCopy = newMaster.Open();
           newMasterCopy.Import(picturePath);
           newMasterCopy.Close();
       }

       [STAThread]
       static void Main(string[] args)
       {
           IVApplication app = new ApplicationClass();
           IVDocument docStencil = app.Documents.AddEx("", VisMeasurementSystem.visMSDefault, (int) VisOpenSaveArgs.visAddStencil, 0);

           ImportPictureAsMaster(docStencil, @"C:\Users\bnk\Pictures\pic1.png");
           ImportPictureAsMaster(docStencil, @"C:\Users\bnk\Pictures\pic2.png");

           docStencil.SaveAs(@"C:\Users\bnk\Pictures\myStencil.vss");
           docStencil.Close();

           app.Quit();
       }

   }
}


yoursashok

Thanks a lot for you example Nikolay. I'm still getting that exception. I will check with the instances and in a different machine. Yes, I do use the right image files and have the destination directory.

Regards,
Ashok.

RodrigoCampos

Quote from: Nikolay on April 15, 2010, 04:37:55 PM
Okay, I just was in a process of "looking at" the C# 4 :)
Here is the code for "normal" C# - it seems to "work on my macnine"... I have checked it against Visio '03, '07 and '10.

Maybe you have Visio instance "hanging" in memory for some reason?
In this case, try killing all such Visio instances prior to running the code.

Please note, this code assumes you do have "pic1.png" and "pic2.png" picture files! Please replace these with YOUR pictures you want to add!
Also it assumes that folder "C:\Users\bnk\Pictures\" does exist! Replace it with a real one :D


using System;
using Microsoft.Office.Interop.Visio;

namespace MyApp
{
    class Program
    {
        static void ImportPictureAsMaster(IVDocument docStencil, string picturePath)
        {
            IVMaster newMaster = docStencil.Masters.Add();
            newMaster.NameU = System.IO.Path.GetFileName(picturePath);

            IVMaster newMasterCopy = newMaster.Open();
            newMasterCopy.Import(picturePath);
            newMasterCopy.Close();
        }

        [STAThread]
        static void Main(string[] args)
        {
            IVApplication app = new ApplicationClass();
            IVDocument docStencil = app.Documents.AddEx("", VisMeasurementSystem.visMSDefault, (int) VisOpenSaveArgs.visAddStencil, 0);

            ImportPictureAsMaster(docStencil, @"C:\Users\bnk\Pictures\pic1.png");
            ImportPictureAsMaster(docStencil, @"C:\Users\bnk\Pictures\pic2.png");

            docStencil.SaveAs(@"C:\Users\bnk\Pictures\myStencil.vss");
            docStencil.Close();

            app.Quit();
        }

    }
}


Dear Nikolay,

I used your code with some few modifications and one of them was this:

// Your Code
Visio.Document docStencil = app.Documents.AddEx("", VisMeasurementSystem.visMSDefault, (int)VisOpenSaveArgs.visAddStencil, 0);

// My code
Visio.Document docStencil = app.Documents.OpenEx("C:\\MyStencil\\Test.vss", (short)Visio.VisOpenSaveArgs.visAddDocked);

but at following line:

Visio.Master newMaster = docStencil.Masters.Add();

I get this error:

"Requested operation is presently disabled."

Why?Is my stencil read-only?

Thank you in advance.

Best Regards,
Rodrigo Campos

Nikolay

Hi, yes, this is most probably the case. By default, Visio opens a "docked"  in read-only mode.
You could try:

Visio.Document docStencil = app.Documents.OpenEx("C:\\MyStencil\\Test.vss", (short)(Visio.VisOpenSaveArgs.visOpenDocked+Visio.VisOpenSaveArgs.visOpenRW));