static void CheckType(DBStructureType t) { Console.WriteLine("Check Type: " + t.Name.ToString() + " (" + t.ID.ToString() + ")"); IQueryable<DBStructure> structs = from s in db.DBStructures where s.TypeID == t.ID select s; SortedDictionary<long, DBStructure> DictStruct = new SortedDictionary<long, DBStructure>(); foreach (DBStructure s in structs) { DictStruct.Add(s.ID, s); } foreach (long ID in DictStruct.Keys) { if (DictStruct.ContainsKey(ID + 1) == false) { continue; } DBStructure structA = DictStruct[ID]; DBStructure structB = DictStruct[ID + 1]; if (structA.ParentID != structB.ParentID) continue; CheckOverlap(t, structA, structB); } }
static void CheckOverlap(DBStructureType t, DBStructure structA, DBStructure structB) { IQueryable<DBLocation> LocsA = from l in db.DBLocations where l.ParentID == structA.ID select l; IQueryable<DBLocation> LocsB = from l in db.DBLocations where l.ParentID == structB.ID select l; List<DBLocation> listA = new List<DBLocation>(); List<DBLocation> listB = new List<DBLocation>(); List<GridVector3> pointsA = new List<GridVector3>(); List<GridVector3> pointsB = new List<GridVector3>(); //Scale values double XYScale = 2.18; double ZScale = 90; foreach(DBLocation l in LocsA) { listA.Add(l); pointsA.Add( new GridVector3(l.VolumeX * XYScale, l.VolumeY * XYScale, l.Z * ZScale) ); } foreach (DBLocation l in LocsB) { listB.Add(l); pointsB.Add(new GridVector3(l.VolumeX * XYScale, l.VolumeY * XYScale, l.Z * ZScale)); } if (listB.Count == 0 || listA.Count == 0) { return; } //Find the nearest point int iA; int iB; double distance; GridVector3.Nearest(pointsA.ToArray(), pointsB.ToArray(), out iA, out iB, out distance); if(distance < listA[iA].Radius + listB[iB].Radius) { Console.WriteLine("[" + structA.ParentID.ToString() + "] " + listA[iA].ParentID.ToString() + " <-> " + listB[iB].ParentID.ToString() + " distance " + distance.ToString()); long newParentID = structB.ID; foreach (DBLocation l in LocsA) { l.ParentID = newParentID; } IQueryable<DBLocationLink> locLinks = from l in db.DBLocationLinks where (l.LinkedFrom == listA[iA].ID && l.LinkedTo == listB[iB].ID) || (l.LinkedFrom == listB[iB].ID && l.LinkedTo == listA[iA].ID) select l; bool ExistingLinkFound = false; foreach (DBLocationLink link in locLinks) { ExistingLinkFound = true; break; } if (ExistingLinkFound == false) { DBLocationLink link = new DBLocationLink(); link.LinkedFrom = listA[iA].ID; link.LinkedTo = listB[iB].ID; link.Username = "******"; db.DBLocationLinks.InsertOnSubmit(link); } //Update structureLinks IQueryable<DBStructureLink> linkFrom = from l in db.DBStructureLinks where l.SourceID == structA.ID select l; foreach(DBStructureLink l in linkFrom) { DBStructureLink newLink = new DBStructureLink(); newLink.SourceID = newParentID; newLink.TargetID = l.TargetID; newLink.Username = l.Username; newLink.Tags = l.Tags; newLink.Bidirectional = l.Bidirectional; db.DBStructureLinks.DeleteOnSubmit(l); DBStructureLink existingLink = (from el in db.DBStructureLinks where el.SourceID == newLink.SourceID && el.TargetID == newLink.TargetID select el).SingleOrDefault<DBStructureLink>(); if(existingLink == null) db.DBStructureLinks.InsertOnSubmit(newLink); } IQueryable<DBStructureLink> linkTo = from l in db.DBStructureLinks where l.TargetID == structA.ID select l; foreach(DBStructureLink l in linkTo) { DBStructureLink newLink = new DBStructureLink(); newLink.SourceID = l.SourceID; newLink.TargetID = newParentID; newLink.Username = l.Username; newLink.Bidirectional = l.Bidirectional; newLink.Tags = l.Tags; db.DBStructureLinks.DeleteOnSubmit(l); DBStructureLink existingLink = (from el in db.DBStructureLinks where el.TargetID == newLink.TargetID && el.SourceID == newLink.SourceID select el).SingleOrDefault<DBStructureLink>(); if (existingLink == null) db.DBStructureLinks.InsertOnSubmit(newLink); } db.DBStructures.DeleteOnSubmit(structA); db.SubmitChanges(); } }
partial void DeleteDBStructureType(DBStructureType instance);
partial void UpdateDBStructureType(DBStructureType instance);
partial void InsertDBStructureType(DBStructureType instance);
public StructureType CreateStructureType(StructureType new_structureType) { try { DBStructureType db_obj = new DBStructureType(); //Create the object to get the ID new_structureType.Sync(db_obj); db.DBStructureTypes.InsertOnSubmit(db_obj); db.Log = Console.Out; db.SubmitChanges(); Console.Out.Flush(); StructureType output_obj = new StructureType(db_obj); return output_obj; } finally { if (db != null) db.Connection.Close(); } return null; }
public long[] Update(StructureType[] structTypes) { IQueryable<DBStructureType> types = from t in db.DBStructureTypes select t; Dictionary<DBStructureType, int> mapNewTypeToIndex = new Dictionary<DBStructureType, int>(structTypes.Length); //Stores the ID of each object manipulated for the return value long[] listID = new long[structTypes.Length]; try { for (int iObj = 0; iObj < structTypes.Length; iObj++) { StructureType t = structTypes[iObj]; switch (t.DBAction) { case DBACTION.INSERT: DBStructureType newType = new DBStructureType(); t.Sync(newType); db.DBStructureTypes.InsertOnSubmit(newType); mapNewTypeToIndex.Add(newType, iObj); break; case DBACTION.UPDATE: DBStructureType updateType; try { updateType = (from u in types where u.ID == t.ID select u).Single(); } catch (System.ArgumentNullException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } catch (System.InvalidOperationException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } t.Sync(updateType); listID[iObj] = updateType.ID; // db.DBStructureTypes.(updateType); break; case DBACTION.DELETE: DBStructureType deleteType; try { deleteType = (from u in types where u.ID == t.ID select u).Single(); } catch (System.ArgumentNullException e) { Debug.WriteLine("Could not find structuretype to delete: " + t.ID.ToString()); break; } catch (System.InvalidOperationException e) { Debug.WriteLine("Could not find structuretype to update: " + t.ID.ToString()); break; } deleteType.ID = t.ID; listID[iObj] = deleteType.ID; db.DBStructureTypes.DeleteOnSubmit(deleteType); break; } } } catch (Exception e) { Debug.WriteLine(e.ToString()); throw e; } db.SubmitChanges(); //Recover the ID's for new objects foreach (DBStructureType newType in mapNewTypeToIndex.Keys) { int iIndex = mapNewTypeToIndex[newType]; listID[iIndex] = newType.ID; } return listID; }