IfcDoc.FormEdit.toolStripMenuItemInsertAttribute_Click C# (CSharp) Method

toolStripMenuItemInsertAttribute_Click() private method

private toolStripMenuItemInsertAttribute_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void toolStripMenuItemInsertAttribute_Click(object sender, EventArgs e)
        {
            // prompt user to select type (or primitive)
            using(FormSelectEntity form = new FormSelectEntity(null, null, this.m_project, SelectDefinitionOptions.Entity | SelectDefinitionOptions.Type))
            {
                if(form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK && form.SelectedEntity != null)
                {
                    TreeNode tnParent = this.treeView.SelectedNode;
                    DocSchema docSchema = (DocSchema)tnParent.Parent.Parent.Tag;
                    DocEntity docEntity = (DocEntity)tnParent.Tag;
                    DocAttribute docAttr = new DocAttribute();
                    docAttr.Name = String.Empty;
                    docAttr.DefinedType = form.SelectedEntity.Name;
                    docEntity.Attributes.Add(docAttr);

                    // is the selected type within the same schema?
                    if ((form.SelectedEntity is DocType && docSchema.Types.Contains((DocType)form.SelectedEntity)) ||
                        (form.SelectedEntity is DocEntity && docSchema.Entities.Contains((DocEntity)form.SelectedEntity)))
                    {
                        docAttr.Definition = form.SelectedEntity;

                        //docAttr.DiagramLine.Add(new DocPoint(docEntity.DiagramRectangle.X + docEntity.DiagramRectangle.Width, docEntity.DiagramRectangle.Y + docEntity.Attributes.IndexOf(docAttr) * 100.0));
                        //docAttr.DiagramLine.Add(new DocPoint(form.SelectedEntity.DiagramRectangle.X, form.SelectedEntity.DiagramRectangle.Y + form.SelectedEntity.DiagramRectangle.Height / 2));
                    }
                    else
                    {
                        // find existing reference
                        DocDefinitionRef docDefTarget = null;
                        foreach(DocSchemaRef docSchemaRef in docSchema.SchemaRefs)
                        {
                            foreach(DocDefinitionRef docDefinitionRef in docSchemaRef.Definitions)
                            {
                                if (docDefinitionRef.Name.Equals(form.SelectedEntity.Name))
                                {
                                    // found it
                                    docDefTarget = docDefinitionRef;
                                    break;
                                }
                            }

                            if (docDefTarget != null)
                                break;
                        }

                        if (docDefTarget == null)
                        {
                            // import it

                            // find the applicable schema...
                            DocSchema docTargetSchema = this.m_project.GetSchemaOfDefinition(form.SelectedEntity);
                            DocSchemaRef docTargetSchemaRef = null;
                            foreach(DocSchemaRef docSchemaRef in docSchema.SchemaRefs)
                            {
                                if(docSchemaRef.Name.Equals(docTargetSchema.Name))
                                {
                                    docTargetSchemaRef = docSchemaRef;
                                    break;
                                }
                            }

                            if (docTargetSchemaRef == null)
                            {
                                docTargetSchemaRef = new DocSchemaRef();
                                docTargetSchemaRef.Name = docTargetSchema.Name;
                                docSchema.SchemaRefs.Add(docTargetSchemaRef);
                            }

                            docDefTarget = new DocDefinitionRef();
                            docDefTarget.Name = form.SelectedEntity.Name;
                            docDefTarget.DiagramRectangle = new DocRectangle();
                            docDefTarget.DiagramRectangle.X = docEntity.DiagramRectangle.X + docEntity.DiagramRectangle.Width + 100;
                            docDefTarget.DiagramRectangle.Y = docEntity.DiagramRectangle.Y + (docEntity.Attributes.Count - 1) * 100;
                            docDefTarget.DiagramRectangle.Width = 400.0;
                            docDefTarget.DiagramRectangle.Height = 50.0;
                            docTargetSchemaRef.Definitions.Add(docDefTarget);

                            docAttr.Definition = docDefTarget;
                        }
                        else
                        {
                            docAttr.Definition = docDefTarget;
                        }

                    }

                    // check if there's a page reference target -- use that if it exists
                    foreach (DocPageTarget docPageTarget in docSchema.PageTargets)
                    {
                        if (docPageTarget.Definition == docAttr.Definition)
                        {
                            DocPageSource docPageSource = new DocPageSource();
                            docPageTarget.Sources.Add(docPageSource);
                            //docPageSource.Target = docPageTarget;
                            docPageSource.DiagramRectangle = new DocRectangle();
                            docPageSource.DiagramRectangle.X = docEntity.DiagramRectangle.X + docEntity.DiagramRectangle.Width + 100;
                            docPageSource.DiagramRectangle.Y = docEntity.DiagramRectangle.Y + (docEntity.Attributes.Count - 1) * 100;
                            docPageSource.DiagramRectangle.Width = 400.0;
                            docPageSource.DiagramRectangle.Height = 50.0;

                            docAttr.Definition = docPageSource;

                            docAttr.DiagramLine.Add(new DocPoint(docEntity.DiagramRectangle.X + docEntity.DiagramRectangle.Width, docPageSource.DiagramRectangle.Y + docPageSource.DiagramRectangle.Height / 2));
                            docAttr.DiagramLine.Add(new DocPoint(docPageSource.DiagramRectangle.X, docPageSource.DiagramRectangle.Y + docPageSource.DiagramRectangle.Height / 2));
                            break;
                        }
                    }

                    if (docAttr.DiagramLine.Count == 0 && docAttr.Definition.DiagramRectangle != null)
                    {
                        docAttr.DiagramLine.Add(new DocPoint(docEntity.DiagramRectangle.X + docEntity.DiagramRectangle.Width, docAttr.Definition.DiagramRectangle.Y + docAttr.Definition.DiagramRectangle.Height / 2));
                        docAttr.DiagramLine.Add(new DocPoint(docAttr.Definition.DiagramRectangle.X, docAttr.Definition.DiagramRectangle.Y + docAttr.Definition.DiagramRectangle.Height / 2));

                        docAttr.DiagramLabel = new DocRectangle();
                        docAttr.DiagramLabel.X = docAttr.DiagramLine[0].X;
                        docAttr.DiagramLabel.Y = docAttr.DiagramLine[0].Y;
                        docAttr.DiagramLabel.Width = 400.0;
                        docAttr.DiagramLabel.Height = 50.0;

                    }

                    this.treeView.SelectedNode = this.LoadNode(tnParent, docAttr, docAttr.ToString(), false, docEntity.Attributes.Count - 1);
                    toolStripMenuItemEditRename_Click(this, e);

                }
            }
        }
FormEdit