using visio to automate drawing diagram with c# - can't set custom properties

Unanswered using visio to automate drawing diagram with c# - can't set custom properties

  • 25 April 2012 12:20
     
      Memiliki Kode

    I have data I am grabbing from a database, and I want to use those values to populate properties on shapes on a visio diagram.  I open my own template, and display shapes on the sheet with no problem.  My server icon has custom properties such as "manufacturer", "network", etc.  When I print out the custom properties for each shape, I get the following output:

    Shape = server Label = Manufacturer Value = 0

    etc.  This is all correct since I haven't set any values yet.  When I go to set the values, using the code in the Visio 2003 SDK (SettingACustomProperty.cs) I get an error that "this shape does not have a custom property with the universal name of Manufacturer" - yet printing out the custom properties clearly shows it does (as does looking at the shapeSheet once the shape is drawn in Visio). I've inserted the code to the SettingACustomProperty.cs code below, as well as how I am calling it.  Can someone help me figure out what I need to do to insert values into my custom properties on a shape?

    Here is how I am calling the code (for testing purposes I have inserted strings myself):

    string keyword = "Manufacturer";
    string manuValue = "\"Dell\"";
    
                string three = SettingACustomProperty.SetCustomPropertyFormula(visioStarShape, keyword, manuValue);

    using System;
    
    namespace drawVisio {
    
    	/// <summary>This class demonstrates how to programmatically set the values
    	/// for custom property types on a shape.</summary>
    	public class SettingACustomProperty {
    
    		/// <summary>This method is the class constructor.</summary>
    		public SettingACustomProperty() {
    
    			// No initialization is required.
    		}
    
    		/// <summary> This method accesses the custom property Value cell and
    		/// sets the formula in that cell to the string in the
    		/// universalSyntaxFormula parameter. When the formula is set, Visio
    		/// evaluates the formula to determine the result for that cell. If the
    		/// formula creates dependencies on other cells, the value of the custom
    		/// property will change when those cells change.</summary>
    		/// <param name="customPropertyShape">Shape that has the custom
    		/// property</param>
    		/// <param name="rowNameU">Universal name of the custom property to be
    		/// accessed</param>
    		/// <param name="universalSyntaxFormula">Value to be assigned to the
    		/// custom property</param>
    		public static string SetCustomPropertyFormula(
    			Microsoft.Office.Interop.Visio.Shape customPropertyShape, 
    			string rowNameU, 
    			string universalSyntaxFormula) {
    
                    Console.WriteLine("value of data passed in is " + universalSyntaxFormula);
    			if (customPropertyShape == null || rowNameU == null
    				|| universalSyntaxFormula == null) {
                        return "yes";
    			}
    
    			const string CUST_PROP_PREFIX = "Prop.";
    
    			Microsoft.Office.Interop.Visio.Cell customPropertyCell;
    			
    			Microsoft.Office.Interop.Visio.Application visioApplication = 
    				(Microsoft.Office.Interop.Visio.Application)
    				customPropertyShape.Application;
    
    			try {
    
    				// Verify that all incoming string parameters are not of zero 
    				// length, except for the ones that have default values as ""
    				// and the output parameters.
    				if (rowNameU.Length == 0) {
    
    					throw new System.ArgumentNullException("rowNameU",
    						"Zero length string input.");
    				}
    
    				if (universalSyntaxFormula.Length == 0) {
    
    					throw new System.ArgumentNullException(
    						"universalSyntaxFormula", "Zero length string input.");
    				}
    
    				// Check to see if the shape has custom property Value cell 
    				// with this universal row name. If no cell exists display 
    				// an error message and exit this method.
    
    				if (customPropertyShape.get_CellExistsU(CUST_PROP_PREFIX + 
    					rowNameU, (short)Microsoft.Office.Interop.Visio.
    					VisExistsFlags.visExistsLocally)== 0) {
    
    					if (visioApplication.AlertResponse == 0) {
    
    						Console.WriteLine(
    							"This shape " + customPropertyShape + " does not have a custom property"
    							+ "\r\nwith the universal name of '" + rowNameU 
    							+ "'.");
    					}
    					
    					return "no";
    				}
    
    				// Get the Cell object. Note the addition of "Prop." to cell name
    				customPropertyCell = customPropertyShape.get_CellsU(
    					CUST_PROP_PREFIX + rowNameU);
    
    				// To see how this method works, try several  different 
    				// formula parameters. For example, if the formula is "5*12"
    				// then the value of the custom property Value cell will be
    				// set to 60. Also, try the formula: "Width * 2".  In this 
    				// case, the value of the custom property  will be set to 
    				// the value of the current width times 2 AND the custom 
    				// property value will change when the width of the shape 
    				// changes.
    				customPropertyCell.FormulaU = universalSyntaxFormula;
    			}
    			catch (Exception err) {
    				System.Diagnostics.Debug.WriteLine(err.Message);
    				throw;
    			}
    
                //test
                return "yes";
    		}
    
    		/// <summary>This method assigns a value to a custom property cell with
    		/// the universal row name, specified via rowNameU parameter, in the
    		/// shape, specified via shape parameter, using the value and units
    		/// passed in as parameters.</summary>
    		/// <param name="customPropertyShape">Shape which has the custom 
    		/// property</param>
    		/// <param name="rowNameU">Universal name of the custom property</param>
    		/// <param name="propertyValue">Value of the custom property</param>
    		/// <param name="units">Units of the value of the custom property
    		/// </param>
    		public void SetCustomPropertyValue(
    			Microsoft.Office.Interop.Visio.Shape customPropertyShape,
    			string rowNameU, 
    			object propertyValue, 
    			Microsoft.Office.Interop.Visio.VisUnitCodes units) {
    
    
    			if (customPropertyShape == null || propertyValue == null ||
    				rowNameU == null) {
    				return;
    			}
    
    			const string CUST_PROP_PREFIX  = "Prop.";
    
    			Microsoft.Office.Interop.Visio.Application visioApplication = 
    				(Microsoft.Office.Interop.Visio.Application)
    				customPropertyShape.Application;
    
    			Microsoft.Office.Interop.Visio.Cell customPropertyCell;
    		
    			StringValueInCell formatHelper;
    
    			try {
    
    				// Verify that all incoming string parameters are not of zero 
    				// length, except for the ones that have default values as ""
    				// and the output parameters.
    				if (rowNameU.Length == 0) {
    
    					throw new System.ArgumentNullException("rowNameU",
    						"Zero length string input.");
    				}
    
    				// See if the shape has a custom property Value cell with the
    				// universal row name. If no cell exists, display an error
    				// message and exit this method.
    				if ((customPropertyShape.get_CellExistsU(CUST_PROP_PREFIX + 
    					rowNameU, (short)Microsoft.Office.Interop.Visio.
    					VisExistsFlags.visExistsLocally)) == 0) {
    				
    					if (visioApplication.AlertResponse == 0) {
    
    						Console.WriteLine(
    							"This shape does not have a Custom Property\r\n" +
    							"with the universal name '" + rowNameU + "'.");
    					}
    				}
    				else {
    
    					// Get the Cell object. Note the addition of "Prop." 
    					// to the name given to the cell.
    					customPropertyCell = customPropertyShape.get_CellsU(
    						CUST_PROP_PREFIX + rowNameU);
    					
    					if (units == Microsoft.Office.Interop.Visio.VisUnitCodes.visUnitsString) 
                        {
    						
    						formatHelper = new StringValueInCell();
    						formatHelper.SetCellValueToString(customPropertyCell, 
    						propertyValue.ToString());		
    }
    else {
    
    // Use the set_Result method to set values other than 
    // string type.
    						customPropertyCell.set_Result(units, Convert.ToDouble(propertyValue, 
    							System.Globalization.CultureInfo.InvariantCulture));
    					}
    				}
    			}
    			catch (Exception err) {
    				System.Diagnostics.Debug.WriteLine(err.Message);
    				throw;
    			}
    		}
    	}
    }

Semua Balasan

  • 25 April 2012 15:57
     
     

    what's the chances your running into a case problem (mixed case vs lower case)?

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

  • 25 April 2012 17:04
     
     
    Do you mean with my variables?  I tried using all caps, all lower-case, and mixed for my keyword "Manufacturer" with no luck.  I also tried the same with CUST_PROP_PREFIX - no luck there either.  One of the reasons I tried to test with Manufacturer is there were no spaces, dashes, etc in the
  • 25 April 2012 20:50
     
     

    The matching of the custom property name and the column name are case sensitive. Where did you get the shape (and which one is it)?

    al


    If this answer solves your problem, please check Mark as Answered. If this answer helps, please click the Vote as Helpful button. Al Edlund Visio MVP

  • 11 Juli 2012 17:37
     
      Memiliki Kode

    I've got a Visio 2010 stencil with a few shapes on it. On the ShapeSheet, the Shape Data columns are things like "Prop.Manufacturer", "Prop.IP" etc.  I want to read data from a database, start a new Visio diagram, draw shapes on the page, and take data from the database and change the properties on each shape.  I've got it all working except once I read the data from the database, I cannot change the custom properties on the shape.  Below is the code I am using.  I have also tried adding "Prop." to the beginning of each row name with no luck.  Can anyway tell me what I am doing wrong?

    public static string SetCustomPropertyFormula( Microsoft.Office.Interop.Visio.Shape customPropertyShape, string rowNameU, string universalSyntaxFormula) 
    { 
    if (customPropertyShape == null || rowNameU == null || universalSyntaxFormula == null)
    
    { return "yes"; 
    } 
    
    const string CUST_PROP_PREFIX = "Prop."; Microsoft.Office.Interop.Visio.Cell customPropertyCell; 
    
    Microsoft.Office.Interop.Visio.Application visioApplication =(Microsoft.Office.Interop.Visio.Application) customPropertyShape.Application; 
    
    try { 
    
    if (rowNameU.Length == 0) 
    
    { throw new System.ArgumentNullException("rowNameU","Zero length string input."); 
    } 
    
    if (universalSyntaxFormula.Length == 0) 
    { 
    throw new System.ArgumentNullException("universalSyntaxFormula", "Zero length string input."); 
    } 
    
    Console.WriteLine("universal name is " + rowNameU); // prints "universal name is Manufacturer" 
    
    if (customPropertyShape.get_CellExistsU(rowNameU, (short)Microsoft.Office.Interop.Visio. VisExistsFlags.visExistsLocally)== 0) 
    
    { if (visioApplication.AlertResponse == 0) 
    
    { Console.WriteLine("Shape does not have a custom property with the universal name of '" + rowNameU + "'.");
    
     } // print "Shape does not have... Manufacturer" return "no"; } 

  • 13 Juli 2012 16:09
    Moderator
     
     
    I just had a thought that you might be looking at the Label cell in the shapesheet and not the row name.  The CellExists method checks against the row name for the shape data field.  It is not checking the Label.  When you look at the shapesheet the row name is the red text in the first cell, e.g., "Prop.Manufacturer". 

    ---------- Fred Diggs Visio Certified Technology Specialist, Visio Application Development Visio 2010 Forum Moderator

  • 17 Juli 2012 11:32
     
     
    The shape is coming from my stencil.  I have a shape called server with Shape Data of "IP", "Location", "Manufacturer", etc. I want to take data from a db and populate these "fields" on the shape diagram