Exemplo n.º 1
0
        public static void DrawModel(DBModel model, bool withViews, bool showVisio, string path, bool physNames, bool allFields)
        {
            if (!System.IO.File.Exists(path + @"Files\SLModel.VSDX"))
            {
                MessageBox.Show("Das Template SLModel.vsdx wurde nicht unter " + path + @"Files\ gefunden. Bitte die README-Datei lesen.");
                return;
            }

            var fokus      = (bool)((MainWindow)Application.Current.MainWindow).Fokus_Aktiv.IsChecked;
            var fokusTable = ((MainWindow)Application.Current.MainWindow).Fokus_Tabelle.Text;

            if (fokus)
            {
                if (fokusTable.Length < 1)
                {
                    MessageBox.Show(@"Bitte Fokus-Tabelle eingeben oder Fokus deaktivieren.");
                    return;
                }
                if (!model.TablesList.Exists(tab => tab.Name.ToLower().Equals(fokusTable.ToLower())))
                {
                    MessageBox.Show(@"Fokus-Tabelle " + fokusTable + " nicht gefunden.");
                    return;
                }
                if (((MainWindow)Application.Current.MainWindow).Fokus_Tiefe.Text.Length < 1)
                {
                    MessageBox.Show(@"Tiefe des Fokus zu kurz.");
                    return;
                }
            }

            try
            {
                var x = -5;
                var y = 11;
                // ToDo: erst berechnen nach Fokus
                var breite  = (int)Math.Sqrt(model.TablesList.Count);
                var hoehe   = 0;
                var counter = 0;

                application         = new Visio.Application();
                application.Visible = showVisio;

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                doc = application.Documents.Open(path + @"Files\SLModel.VSDX");
                //doc = application.Documents.Open(Environment.CurrentDirectory + @"\Files\SLModel.VSDX");
                doc.SaveAs(path + (fokus ? "Auschnittsfokus_" + fokusTable + "_" : "") + model.serverName + ".vsdx");
                doc.Creator = @"Hicham Ait Ayad";
                doc.Title   = model.serverName;

                page                 = doc.Pages[1];
                page.Name            = @"Scopeland DB Model Builder";
                page.AutoSize        = true;
                visioEntityMaster    = doc.Masters.get_ItemU(@"Entity");
                visioAttributeMaster = doc.Masters.get_ItemU(@"Attribute");
                visioConnectorMaster = doc.Masters.get_ItemU(@"Relationship");

                /***************************************************************************
                * ****************************** FOKUS ************************************
                ***************************************************************************/
                if (fokus)
                {
                    //var fokusTable = ((MainWindow)Application.Current.MainWindow).Fokus_Tabelle.Text.ToLower();
                    var fokusDepth = Int32.Parse(((MainWindow)Application.Current.MainWindow).Fokus_Tiefe.Text);
                    //var mandanten = "t_mandanten";

                    List <Table> TablesToLeaveList = new List <Table>();

                    model.RelationsList.FindAll(rel =>
                                                //!rel.FromField.Table.Name.ToLower().Equals(mandanten) &&
                                                //!rel.ToField.Table.Name.ToLower().Equals(mandanten) &&
                                                (rel.FromField.Table.Name.ToLower().Equals(fokusTable.ToLower()) ||
                                                 rel.ToField.Table.Name.ToLower().Equals(fokusTable.ToLower())))
                    .ForEach(rel =>
                    {
                        TablesToLeaveList.Add(rel.FromField.Table);
                        TablesToLeaveList.Add(rel.ToField.Table);
                    }

                             );

                    for (var i = 0; i < fokusDepth - 1; i++)
                    {
                        List <Table> tablesListTemp = new List <Table>();

                        TablesToLeaveList.ForEach(table => model.RelationsList.FindAll(rel => rel.FromField.Table.Equals(table)).ForEach(rel2 =>
                        {
                            tablesListTemp.Add(rel2.FromField.Table);
                            tablesListTemp.Add(rel2.ToField.Table);
                        }));

                        TablesToLeaveList.AddRange(tablesListTemp);
                    }


                    model.TablesList.RemoveAll(table => !TablesToLeaveList.Contains(table));
                }
                /***************************************************************************/

                /***************************************************************************
                * ****************************** Black/Whitelists ************************************
                ***************************************************************************/
                List <string> tableBlacklist = new List <string>(((MainWindow)Application.Current.MainWindow).Table_Blacklist_Text.Text.Trim().Split(','));
                List <string> tableWhitelist = new List <string>(((MainWindow)Application.Current.MainWindow).Table_Whitelist_Text.Text.Trim().Split(','));

                if (tableBlacklist.Count > 0 && !tableBlacklist[0].Equals(""))
                {
                    model.TablesList.RemoveAll(table => tableBlacklist.Contains(table.Name));
                }

                if (tableWhitelist.Count > 0 && !tableWhitelist[0].Equals(""))
                {
                    model.TablesList.RemoveAll(table => !tableWhitelist.Contains(table.Name));
                }
                /***************************************************************************/

                foreach (Table table in model.TablesList)
                {
                    if (withViews || !table.KindOfObject.Trim().Equals("View"))
                    {
                        if (counter++ % breite == 0)
                        {
                            y    -= hoehe / 2;
                            hoehe = 0;
                            x     = -5;
                        }
                        Visio.Shape entity = page.Drop(visioEntityMaster, x += 5, y);

                        if (allFields && table.Fields.Count > hoehe)
                        {
                            hoehe = table.Fields.Count;
                        }
                        else
                        if (!allFields)
                        {
                            foreach (Field field in table.Fields)
                            {
                                hoehe += model.FieldList.Contains(field) ? 1 : 0;
                            }
                        }


                        Array members = entity.ContainerProperties.GetListMembers();
                        foreach (int member in members)
                        {
                            entity.Shapes.ItemFromID[member].Delete();
                        }
                        //tablesID.Add(table.Name, entity.ID);
                        //printProperties(entity.Shapes);
                        entity.Text = physNames ? table.Name : table.NameD;
                        int i = 1;


                        foreach (Field field in table.Fields)
                        {
                            if (allFields || model.FieldList.Contains(field))
                            {
                                Visio.Shape attribute = page.Drop(visioAttributeMaster, 0, 0);
                                field.ShapeID = attribute.UniqueID[(short)Visio.VisUniqueIDArgs.visGetOrMakeGUID];
                                //fieldsID.Add(field.Table.Name + "_" + field.Name, attribute.ID);
                                attribute.Text = physNames ? field.Name : field.NameD;
                                entity.ContainerProperties.InsertListMember(attribute, i++);
                                //entity.ContainerProperties.AddMember(visioAttributeMaster, Visio.VisMemberAddOptions.visMemberAddUseResizeSetting);
                            }
                        }
                    }
                }
                page.CreateSelection(Visio.VisSelectionTypes.visSelTypeAll).Layout();

                foreach (Relation relation in model.RelationsList)
                {
                    if (relation.FromField.ShapeID != null && relation.ToField.ShapeID != null && relation.TypeFrom != null && relation.TypeTo != null)
                    {
                        int index;
                        if ((relation.TypeFrom.Substring(3).Equals(">>") && relation.TypeTo.Substring(3).Equals("->")) || (relation.TypeFrom.Substring(3).Equals("->") && relation.TypeTo.Substring(3).Equals(">>")))
                        {
                            fromField = relation.TypeFrom.Substring(3).Equals(">>") ? page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID] : page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID];
                            toField   = relation.TypeFrom.Substring(3).Equals(">>") ? page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID] : page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID];
                            index     = 0;
                        }
                        else
                        {
                            if (relation.TypeFrom.Substring(3).Equals(">>") && relation.TypeTo.Substring(3).Equals(">>"))
                            {
                                fromField = page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID];
                                toField   = page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID];
                                index     = 2;
                            }
                            else
                            {
                                fromField = page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID];
                                toField   = page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID];
                                index     = 1;
                            }
                        }
                        ConnectWithDynamicGlueAndConnector(fromField, toField, index);
                    }
                }
                page.AutoSizeDrawing();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                doc.Save();
                if (showVisio)
                {
                    application.Quit();
                }
            }
            MessageBox.Show("All done!");
        }
Exemplo n.º 2
0
        public static void DrawModel(Model.DBModel model, bool withViews, bool showVisio, string path, bool physNames, bool allFields)
        {
            if (!System.IO.File.Exists(path + @"Files\SLModel.VSDX"))
            {
                System.Windows.MessageBox.Show("Das Template SLModel.vsdx wurde nicht unter " + path + @"Files\ gefunden. Bitte die README-Datei lesen.");
                return;
            }

            try
            {
                var x       = -5;
                var y       = 11;
                var breite  = (int)Math.Sqrt(model.TablesList.Count);
                var hoehe   = 0;
                var counter = 0;

                application         = new Visio.Application();
                application.Visible = showVisio;

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                doc = application.Documents.Open(path + @"Files\SLModel.VSDX");
                //doc = application.Documents.Open(Environment.CurrentDirectory + @"\Files\SLModel.VSDX");
                doc.SaveAs(path + model.serverName + ".vsdx");
                doc.Creator = @"Hicham Ait Ayad";
                doc.Title   = model.serverName;

                page                 = doc.Pages[1];
                page.Name            = @"Scopeland DB Model Builder";
                page.AutoSize        = true;
                visioEntityMaster    = doc.Masters.get_ItemU(@"Entity");
                visioAttributeMaster = doc.Masters.get_ItemU(@"Attribute");
                visioConnectorMaster = doc.Masters.get_ItemU(@"Relationship");

                foreach (Model.Table table in model.TablesList)
                {
                    if (withViews || !table.KindOfObject.Trim().Equals("View"))
                    {
                        if (counter++ % breite == 0)
                        {
                            y    -= hoehe / 2;
                            hoehe = 0;
                            x     = -5;
                        }
                        Visio.Shape entity = page.Drop(visioEntityMaster, x += 5, y);

                        if (allFields && table.Fields.Count > hoehe)
                        {
                            hoehe = table.Fields.Count;
                        }
                        else
                        if (!allFields)
                        {
                            foreach (Model.Field field in table.Fields)
                            {
                                hoehe += model.FieldList.Contains(field) ? 1 : 0;
                            }
                        }


                        Array members = entity.ContainerProperties.GetListMembers();
                        foreach (int member in members)
                        {
                            entity.Shapes.ItemFromID[member].Delete();
                        }
                        //tablesID.Add(table.Name, entity.ID);
                        //printProperties(entity.Shapes);
                        entity.Text = physNames ? table.Name : table.NameD;
                        int i = 1;


                        foreach (Model.Field field in table.Fields)
                        {
                            if (allFields || model.FieldList.Contains(field))
                            {
                                Visio.Shape attribute = page.Drop(visioAttributeMaster, 0, 0);
                                field.ShapeID = attribute.UniqueID[(short)Visio.VisUniqueIDArgs.visGetOrMakeGUID];
                                //fieldsID.Add(field.Table.Name + "_" + field.Name, attribute.ID);
                                attribute.Text = physNames ? field.Name : field.NameD;
                                entity.ContainerProperties.InsertListMember(attribute, i++);
                                //entity.ContainerProperties.AddMember(visioAttributeMaster, Visio.VisMemberAddOptions.visMemberAddUseResizeSetting);
                            }
                        }
                    }
                }
                page.CreateSelection(Visio.VisSelectionTypes.visSelTypeAll).Layout();

                foreach (Model.Relation relation in model.RelationsList)
                {
                    if (relation.FromField.ShapeID != null && relation.ToField.ShapeID != null && relation.TypeFrom != null && relation.TypeTo != null)
                    {
                        int index;
                        if ((relation.TypeFrom.Substring(3).Equals(">>") && relation.TypeTo.Substring(3).Equals("->")) || (relation.TypeFrom.Substring(3).Equals("->") && relation.TypeTo.Substring(3).Equals(">>")))
                        {
                            fromField = relation.TypeFrom.Substring(3).Equals(">>") ? page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID] : page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID];
                            toField   = relation.TypeFrom.Substring(3).Equals(">>") ? page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID] : page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID];
                            index     = 0;
                        }
                        else
                        {
                            if (relation.TypeFrom.Substring(3).Equals(">>") && relation.TypeTo.Substring(3).Equals(">>"))
                            {
                                fromField = page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID];
                                toField   = page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID];
                                index     = 2;
                            }
                            else
                            {
                                fromField = page.Shapes.ItemFromUniqueID[relation.FromField.ShapeID];
                                toField   = page.Shapes.ItemFromUniqueID[relation.ToField.ShapeID];
                                index     = 1;
                            }
                        }
                        ConnectWithDynamicGlueAndConnector(fromField, toField, index);
                    }
                }
                page.AutoSizeDrawing();
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.Message);
            }
            finally
            {
                doc.Save();
                if (showVisio)
                {
                    application.Quit();
                }
            }
            System.Windows.MessageBox.Show("All done!");
        }