Exemplo n.º 1
0
        private static void CreateTemplatesFromShapeTypes(Project project, IList <String> shapeTypeNames, Int32 shapeSize, Boolean withModels, Boolean withTerminalMappings, Boolean withModelMappings, Int32 expectedShapeCount)
        {
            if (shapeTypeNames == null)
            {
                shapeTypeNames = new List <String>();
                shapeTypeNames.Add("Circle");
                shapeTypeNames.Add("PolyLine");
            }
            //
            foreach (String shapeTypeName in shapeTypeNames)
            {
                ShapeType shapeType = project.ShapeTypes[shapeTypeName];
                // Create a shape for the template
                Shape shape = shapeType.CreateInstance();
                shape.Fit(0, 0, shapeSize, shapeSize);
                // Create the template
                Template template = new Template(String.Format("{0} Template", shapeType.Name), shapeType.CreateInstance());
                if (shape is IPlanarShape)
                {
                    // Add optional data
                    if (withModels)
                    {
                        template.Shape.ModelObject = project.ModelObjectTypes["Core.GenericModelObject"].CreateInstance();
                        template.MapTerminal(TerminalId.Generic, ControlPointId.Reference);
                        if (withTerminalMappings)
                        {
                            foreach (ControlPointId id in template.Shape.GetControlPointIds(ControlPointCapabilities.Connect))
                            {
                                template.MapTerminal(TerminalId.Generic, id);
                            }
                        }
                        if (withModelMappings)
                        {
                            //
                            // Create ModelMappings
                            List <IModelMapping> modelMappings = new List <IModelMapping>(3);
                            // Create numeric- and format model mappings
                            NumericModelMapping numericMapping = new NumericModelMapping(2, 4, NumericModelMapping.MappingType.FloatInteger, 10, 0);
                            FormatModelMapping  formatMapping  = new FormatModelMapping(4, 2, FormatModelMapping.MappingType.StringString, "{0}");
                            // Create style model mapping
                            float             range             = expectedShapeCount / 15f;
                            StyleModelMapping styleModelMapping = new StyleModelMapping(1, 4, StyleModelMapping.MappingType.FloatStyle);
                            for (int i = 0; i < 15; ++i)
                            {
                                IStyle style = null;
                                switch (i)
                                {
                                case 0: style = project.Design.LineStyles.None; break;

                                case 1: style = project.Design.LineStyles.Dotted; break;

                                case 2: style = project.Design.LineStyles.Dashed; break;

                                case 3: style = project.Design.LineStyles.Special1; break;

                                case 4: style = project.Design.LineStyles.Special2; break;

                                case 5: style = project.Design.LineStyles.Normal; break;

                                case 6: style = project.Design.LineStyles.Blue; break;

                                case 7: style = project.Design.LineStyles.Green; break;

                                case 8: style = project.Design.LineStyles.Yellow; break;

                                case 9: style = project.Design.LineStyles.Red; break;

                                case 10: style = project.Design.LineStyles.HighlightDotted; break;

                                case 11: style = project.Design.LineStyles.HighlightDashed; break;

                                case 12: style = project.Design.LineStyles.Highlight; break;

                                case 13: style = project.Design.LineStyles.HighlightThick; break;

                                case 14: style = project.Design.LineStyles.Thick; break;

                                default: style = null; break;
                                }
                                if (style != null)
                                {
                                    styleModelMapping.AddValueRange(i * range, style);
                                }
                            }
                            modelMappings.Add(styleModelMapping);
                            //
                            foreach (IModelMapping modelMapping in modelMappings)
                            {
                                template.MapProperties(modelMapping);
                            }
                        }
                    }
                }
                else if (shape is ILinearShape)
                {
                    // Nothing else to do
                }
                else
                {
                    throw new NotImplementedException();
                }
                // Insert the template into the repository
                project.Repository.InsertAll(template);
            }
            Assert.AreEqual(EnumerationHelper.Count(project.Repository.GetTemplates()), shapeTypeNames.Count);
        }