Programmatically Creating a Line Pattern

Started by saveenr, January 05, 2009, 02:59:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

saveenr

Am trying to create a line pattern via code. So far, I can create the master that represents the line pattern - it is visible in the Drawing Explorer under "Line Patterns" - *but* in the UI to select line patterns ( right-click shape, select "Format/Line" and you'll see the new line pattern does not appear in the "Line" dialog).

Below is the C# code I am using. Any ideas what one needs to do to make the line pattern show up in that UI?

            var doc = this.Shell.visapp.ActiveDocument;

            var master = doc.Masters.Add();
            master.Name = "TaperingLine";
            master.Prompt = "";
            master.IconSize = (short) IVisio.VisMasterProperties.visNormal;
            master.PatternFlags =
                (short) (IVisio.VisMasterProperties.visMasIsLinePat | IVisio.VisMasterProperties.visMasLPStretch);
            master.AlignName = (short) IVisio.VisMasterProperties.visCenter;
            master.MatchByName = VisioAutomation.Conversion.BoolToShort(false);
            master.IconUpdate = (short) IVisio.VisMasterProperties.visAutomatic;

            var master_copy = master.Open();
            var s = master.DrawLine(0, 2, 2, 1);
            s.DrawLine(2.236068, 0, 0.894427, -1.788854);
            s.DrawLine(0, 0, 0, 2);
            master_copy.Close();


Visio Guy

Hi Saveen,

This worked for me, in VBA:

Sub CreateLinePattern()

  Dim mst As Visio.Master
  Dim mstCopy As Visio.Master
 
  Set mst = ThisDocument.Masters.AddEx(Visio.VisMasterTypes.visTypeLinePattern)

  mst.Name = "Scruffy the wonder pattern"
   
  mst.PatternFlags = Visio.VisMasterProperties.visMasIsLinePat Or _
                     Visio.VisMasterProperties.visMasLPStretch
   
  Set mstCopy = mst.Open
 
    Call mstCopy.DrawRectangle(0, 0, 0.5, 0.125)
 
  mstCopy.Close
 
End Sub


The use of AddEx vs. Add didn't seem to matter. And it shows up in my UI just fine.

No idea why your c# isn't working...
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

saveenr

#2
Thanks for the assist! I looked into it some more - the creation of the line pattern seems sensitive to how the shape is drawn. So, when I used your code which calls DrawRectangle (as you did in your example) it worked, but the using my original method of drawing did not. Am not sure why there should be a difference. If I discover more I will share it in this forum.

Visio Guy

Funny, I tested 1D vs. 2D shapes in VBA as well, because that looked suspicious to me.

Both worked fine, and the patterns showed in the UI drop-down lists.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

wapperdude

Rhetorical question, in the C# code, do the line segments need to be grouped or joined to make a single object?  Just curious.
Visio 2019 Pro

Visio Guy

Grouping the shapes in a master is generally a good idea for performance reasons.

If you don't do it, the Visio will do it "on drop", which will slow things down.

However, custom pattern is probably just analyzed for its graphics at some point (just one?) in time.

None of the smarts in the pattern are used in any way, nor are their references from shapes on the page to the master, in the traditional master-shape-inheritance sense. It is just "dumb graphics" in this case.

So my guess is that it probably doesn't really matter much for patterns.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010