visio二次开发——图纸解析之形状

  今天有空,下班前补齐解析visio图形形状的方法,包含图形背景色、字体颜色、备注信息、形状数据取值。

 /// <summary>
        /// 设置形状的选择属性
        /// </summary>
        /******************************************************
         * 0 仅选择组合形状。
         * 1 首先选择组合形状
         * 2 首先选择组合的组成部分 
        ******************************************************/
        public static void SetGroupSelectMode(Shape targetShape, int selectMode)
        {
            targetShape.get_CellsSRC((short)VisSectionIndices.visSectionObject, (short)VisRowIndices.visRowGroup,
                   (short)VisCellIndices.visGroupSelectMode).FormulaU = selectMode.ToString();
        }
View Code

设置打开组合的器件,然后开始取数据:

               if (sp.Shapes.Count > 0)//组合类型      
                            {
                                missDevice = false;
                                ShapeInfo spG = new ShapeInfo();
                                spG.Type = "器件";
                                spG.DeviceName = sp.Name.Split('.')[0];
                                spG.DeviceDetail = "形状数据名:"+sp.Name+"";
                                addGroup(sp,spG);        //拆分组合

                                SetGroupSelectMode(sp, 0);
                                spG.DeviceDetail += GetShapeCellProp(sp);
                                spG.Position = GetShaplocationInfo(sp);
                                spG.DeviceType = getTypeByBgColor(sp);

                       
                                if (spG.DeviceName != "")
                                {
                                    visioInfoDic[PageName].Add(spG);
                                }
                            }
addGroup方法
 private void addGroup(Shape sp,ShapeInfo spi)
        {
            foreach (Shape childSP in sp.Shapes)
            {
                /*线类型*/
                if (childSP.Connects.Count > 0)
                {
                    ShapeLine spL = new ShapeLine();
                    spL.LPosition = GetShaplocationInfo(sp);  //位置信息
                    addLine(sp, spL, true);
                    spi.DeviceName = "";
                    visioLineDic[PageName].Add(spL);
                    break;
                }
                /*器件类型*/
                ShapeInfo spchild = new ShapeInfo();
                SetGroupSelectMode(childSP, 2);
                if (childSP.Shapes.Count > 0)
                    addGroup(childSP, spi);
                SetGroupSelectMode(childSP, 0);

                /* 根据颜色判断*/
                if(spi.DeviceType==null||spi.DeviceType=="")
                    spi.DeviceType = getTypeByBgColor(childSP);
                if(childSP.Text.Contains("dB"))
                     getDWordColor(childSP,childSP.Text,spi);

                if (childSP.Text.Contains("F"))
                {
                    spi.DeviceNum = childSP.Text.Split('/')[0];
                }
                spchild.DeviceName = childSP.Text;
                if (spchild.DeviceName != "")
                {
                    spi.Label = (spi.Label == "") ? spchild.DeviceName : spi.Label + "" + spchild.DeviceName;
                }
            }
          
        }
View Code
GetShapeCellProp读取形状数据的信息
  /// <summary>
        /// 获取图形属性
        /// </summary>
        private static string GetShapeCellProp(Shape shapeTarget)
        {
            string info = "";
            for (int i = 0; i < shapeTarget.get_RowCount((short)VisSectionIndices.visSectionProp); i++)
            {
                Cell cellKey = shapeTarget.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)i, (short)2);
                Cell cellValue = shapeTarget.get_CellsSRC((short)VisSectionIndices.visSectionProp, (short)i, (short)VisCellIndices.visUserValue);
                if (i > 0)
                    info += "";
                info += FormulaForString(cellKey.Formula) + "" + FormulaForString(cellValue.Formula);
            }
            return info;
        }
View Code
GetShaplocationInfo读取位置信息,前一篇已经给出。

获取文字颜色:
  if (sp.get_RowCount((short)VisSectionIndices.visSectionCharacter) == 1)
            {
                /*一个文本单个单颜色*/
                Cell wordCell = sp.get_CellsSRC((short)VisSectionIndices.visSectionCharacter, 0, (short)VisCellIndices.visCharacterColor);
                wordColor = wordCell.Formula;
                deviceType(si, wordColor,power.Replace("/",""));
            }
//多种颜色则循环取出
 for (short i = 0; i < sp.get_RowCount((short)VisSectionIndices.visSectionCharacter); i++)
                    {
                        Cell wordCells = sp.get_CellsSRC((short)VisSectionIndices.visSectionCharacter, i, (short)VisCellIndices.visCharacterColor);
                       if (wordCells.Formula.Contains("THEMEVAL()") && ((sp.get_RowCount((short)VisSectionIndices.visSectionCharacter) - 1) == i))
                        {
                            break;
                        }
                        if (i > 0)
                            wordColor += "|";
                        wordColor += wordCells.Formula;
                    }
View Code

图形背景色:

Cell color = sp.get_CellsSRC((short)VisSectionIndices.visSectionObject, (short)VisRowIndices.visRowFill, (short)VisCellIndices.visFillForegnd);
            string strColor = color.Formula;
View Code

  大多数据都可以通过像取背景色一样,将形状的属性取出,比如线段粗细、字体、字体大小等等,有兴趣的继续研究。

 
 
 
posted @ 2015-12-03 17:50  山治先生  阅读(1463)  评论(1编辑  收藏  举报