/// <summary> /// Adds a child KmlItem to this nodes lists of children, depending of its /// derived class KmlNode, KmlAttrib or further derived from these. /// When an KmlAttrib "Name" is found, its value /// will be used for the corresponding property of this node. /// </summary> /// <param name="item">The KmlItem to add</param> public override void Add(KmlItem item) { KmlItem newItem = item; if (item is KmlAttrib) { KmlAttrib attrib = (KmlAttrib)item; if (attrib.Name.ToLower() == "uid") { Uid = attrib.Value; attrib.AttribValueChanged += Uid_Changed; attrib.CanBeDeleted = false; } else if (attrib.Name.ToLower() == "parent") { int p = ParentPartIndex; if (int.TryParse(attrib.Value, out p)) { ParentPartIndex = p; } else { Syntax.Warning(this, "Unreadable parent part: " + attrib.ToString()); } attrib.AttribValueChanged += ParentPart_Changed; attrib.CanBeDeleted = false; } else if (attrib.Name.ToLower() == "attn") { // Value looks like "top, 12", "bottom, -1", "left, 1", "top2, 3", etc. string[] items = attrib.Value.Split(new char[] { ',' }); int index = -1; if (items.Count() == 2 && int.TryParse(items[1], out index)) { if (index >= 0) { AttachedToNodeIndices.Add(index); attrib.CanBeDeleted = false; } } else { Syntax.Warning(this, "Bad formatted part node attachment: " + attrib.ToString()); } attrib.AttribValueChanged += AttachmentNode_Changed; } else if (attrib.Name.ToLower() == "srfn") { // Value looks like "srfAttach, 12" string[] items = attrib.Value.Split(new char[] { ',' }); int index = -1; if (items.Count() == 2 && int.TryParse(items[1], out index)) { if (index >= 0) { if (AttachedToSurfaceIndex < 0) { AttachedToSurfaceIndex = index; attrib.CanBeDeleted = false; } else { Syntax.Warning(this, "More than one surface attachment is not allowed, already attached to [" + AttachedToSurfaceIndex + "], could not attach to [" + index + "]"); } } } else { Syntax.Warning(this, "Bad formatted part surface attachment: " + attrib.ToString()); } attrib.AttribValueChanged += AttachmentSurface_Changed; } else if (attrib.Name.ToLower() == "position") { // Value looks like "0.1,0,-0.3E-07" string[] items = attrib.Value.Split(new char[] { ',' }); double x = 0; double y = 0; double z = 0; if (items.Count() == 3 && double.TryParse(items[0], NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out x) && double.TryParse(items[1], NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out y) && double.TryParse(items[2], NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out z)) { Position = new Point3D(x, y, z); attrib.CanBeDeleted = false; } else { Syntax.Warning(this, "Bad formatted part position: " + attrib.ToString()); } } } else if (item is KmlResource) { KmlResource res = (KmlResource)item; Resources.Add(res); ResourceTypes.Add(res.Name); // Get notified when Resources change res.MaxAmount.AttribValueChanged += Resources_Changed; res.MaxAmount.CanBeDeleted = false; res.Amount.AttribValueChanged += Resources_Changed; res.Amount.CanBeDeleted = false; } /*if (Item is KmlNode) * { * KmlNode node = (KmlNode)Item; * if (node.Tag.ToLower() == "resource") * { * newItem = new KmlResource(node); * } * }*/ base.Add(newItem); }