private void lDuplicatePartBtn_Click(object sender, EventArgs e) { if ((lDesignObject != null) && (lCurrDesignElement != null)) { // add generic new part to design object DesignElementRect newrect = (DesignElementRect)lCurrDesignElement.Duplicate(); newrect.Name = "New Rect"; // GWH UNFINISHED CODE--FORCE UNIQUE NAME lDesignObject.Elements.Add(newrect); lPartsLvw.BeginUpdate(); lPartsLvwClearSelection(); ListViewItem newlv = lAddDesignElement(newrect); newlv.Selected = true; lPartsLvw.EndUpdate(); // force redraw, add item to listview and set listview selected index to the new item newrect.UpdatePoints(); lDesignCtrl.ForceRedraw(); lCurrDesignElement = newrect; } }
public static DesignObject LoadDesignObject(string filename) { DesignObject dobj = null; string[] filetext = FileHandler.LoadFile(filename); if ((filetext != null) && (filetext.Length > 0)) { char[] eqdelim = new char[] { '=' }; char[] commadelim = new char[] { ',' }; char[] quotedelim = new char[] { '"' }; bool error = false; DesignElement currelement = null; int posndx = 0; dobj = new DesignObject(); foreach (string fileline in filetext) { string linetrim = fileline.Trim(); if ((linetrim.Length > 0) && (linetrim[0] != ';')) { string[] linetokens = linetrim.Split(eqdelim); if (linetokens.Length == 2) { string varname = linetokens[0].ToLower(); if (currelement != null) { if (varname.Equals("name")) { currelement.Name = linetokens[1]; } else if (varname.Equals("objectend")) { currelement = null; } else if (currelement.ElementType == DesignElement.ElementTypes.Rect) { DesignElementRect rectelem = (DesignElementRect)currelement; if (varname.Equals("dimensions")) { string[] dimstrs = linetokens[1].Split(commadelim); if (dimstrs.Length == 3) { try { rectelem.DimensionX = Convert.ToDouble(dimstrs[0]); rectelem.DimensionY = Convert.ToDouble(dimstrs[1]); rectelem.DimensionZ = Convert.ToDouble(dimstrs[2]); rectelem.UpdatePoints(); } catch (System.FormatException /*fex*/) { error = true; } } else { error = true; } } else if (varname.Equals("attach")) { string[] namestrs = linetokens[1].Split(quotedelim); if (namestrs.Length == 3) { string[] ownstrs = namestrs[0].Split(commadelim, StringSplitOptions.RemoveEmptyEntries); string[] attrstrs = namestrs[2].Split(commadelim, StringSplitOptions.RemoveEmptyEntries); if ((ownstrs.Length == 3) && (attrstrs.Length == 3)) { DesignAttachmentParameters dattach = new DesignAttachmentParameters(namestrs[1]); if (!dattach.SetAttachmentParametersFromStrings(attrstrs)) { error = true; } else if (!dattach.SetOwnParametersFromStrings(ownstrs)) { error = true; } rectelem.AttachmentPoint = dattach; } else { error = true; } } else { error = true; } } else if (varname.Equals("midpoint")) { string[] midstrs = linetokens[1].Split(commadelim); if (midstrs.Length == 3) { try { double midx = Convert.ToDouble(midstrs[0]); double midy = Convert.ToDouble(midstrs[1]); double midz = Convert.ToDouble(midstrs[2]); rectelem.Midpoint.SetPoint(midx, midy, midz); rectelem.UpdatePoints(); } catch (System.FormatException /*fex*/) { error = true; } } else { error = true; } } else if (varname.Equals("position")) { string[] posstrs = linetokens[1].Split(commadelim); if ((posstrs.Length == 3) && (posndx < rectelem.Points.Length)) { try { double midx = Convert.ToDouble(posstrs[0]); double midy = Convert.ToDouble(posstrs[1]); double midz = Convert.ToDouble(posstrs[2]); rectelem.Points[posndx].SetPoint(midx, midy, midz); posndx++; } catch (System.FormatException /*fex*/) { error = true; } } else { error = true; } } else { error = true; } } else { error = true; } } else if (varname.Equals("objectname")) { dobj.Name = linetokens[1]; } else if (varname.Equals("measure")) { Units unitval; if (Enum.TryParse(linetokens[1], true, out unitval) && Enum.IsDefined(typeof(Units), unitval)) { dobj.UnitType = unitval; } else { error = true; } } else if (varname.Equals("objectstart")) { if (currelement == null) { DesignElement.ElementTypes etype; if (Enum.TryParse(linetokens[1], true, out etype) && Enum.IsDefined(typeof(DesignElement.ElementTypes), etype)) { switch (etype) { case DesignElement.ElementTypes.Rect: currelement = new DesignElementRect(); break; default: error = true; break; } if (currelement != null) { dobj.Elements.Add(currelement); posndx = 0; } } else { error = true; } } else { error = true; } } } else { error = true; } } if (error) { break; } } if (error) { dobj = null; } else { // fill in any relative attachment point classes if (dobj.Elements.Count > 0) { List <DesignElement> attelems = new List <DesignElement>(); // make a list of all elements that have a primary attachment foreach (DesignElement tmpdelem in dobj.Elements) { lUpdateAttachmentPoints(dobj, tmpdelem); } } } } return(dobj); }