https://gis.stackexchange.com/questions/58245/generate-polygons-from-a-set-of-intersecting-lines
https://i.stack.imgur.com/UUyHF.png
https://i.stack.imgur.com/3ClRI.png
I want to find the bounding polygon like this link.
I'm using C# nettopologysuite like the jts library shown in this link, but the polygon I want doesn't come out.
How can I extract polygons?
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions pEntOpt1 = new PromptEntityOptions("Outter Boundary");
pEntOpt1.SetRejectMessage("is Not Polyline");
pEntOpt1.AddAllowedClass(typeof(Polyline), true);
PromptEntityResult pEntRes1 = ed.GetEntity(pEntOpt1);
string typeName = RXObject.GetClass(typeof(Polyline)).DxfName;
TypedValue[] tv = new TypedValue[1] { new TypedValue((int)DxfCode.Start, typeName) };
SelectionFilter sf = new SelectionFilter(tv);
PromptSelectionResult pSelectRes = ed.GetSelection(sf);
if (pSelectRes.Status != PromptStatus.OK)
return;
ObjectId boudaryPlineId = pEntRes1.ObjectId;
ObjectId[] innerPlineids = pSelectRes.Value.GetObjectIds();
ObjectIdCollection innerBoundaries = new ObjectIdCollection(innerPlineids);
try
{
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Polygonizer polygonizer = new Polygonizer();
GeometryFactory gf = new GeometryFactory();
Polyline boundaryPline = tr.GetObject(boudaryPlineId, OpenMode.ForWrite) as Polyline;
Coordinate[] coords = new Coordinate[boundaryPline.NumberOfVertices + 1];
for (int j = 0; j < boundaryPline.NumberOfVertices; j++)
{
Point3d pos = boundaryPline.GetPoint3dAt(j);
coords[j] = new Coordinate(pos.X, pos.Y);
}
coords[boundaryPline.NumberOfVertices] = new Coordinate(boundaryPline.StartPoint.X, boundaryPline.StartPoint.Y);
Polygon pg = gf.CreatePolygon(coords);
List<Geometry> plines = new List<Geometry>();
foreach (ObjectId indexid in innerBoundaries)
{
Polyline indexPline = tr.GetObject(indexid, OpenMode.ForWrite) as Polyline;
Coordinate[] coords2 = new Coordinate[indexPline.NumberOfVertices];
for (int i = 0; i < indexPline.NumberOfVertices; i++)
{
Point3d pos = indexPline.GetPoint3dAt(i);
coords2[i] = new Coordinate(pos.X, pos.Y);
}
LineString ls1 = new LineString(coords2);
polygonizer.Add(ls1);
}
ICollection<Geometry> test = polygonizer.GetPolygons();
foreach (Geometry polygon in test)
{
// to do
}
tr.Commit();
}
}
}
catch
{
}
outter Boundary
inner Boundary
The Red Polygon I want to find
This is my second question in English, so please understand the lack of expression.
-------------- After applying the answer below --------------
private List<NetGeometry> GetPolygons(Transaction tr, List<LineString> lineStrings)
{
List<NetGeometry> polygons = new List<NetGeometry>();
Polygonizer polygonizer = new Polygonizer(false);
GeometryFactory gf = new GeometryFactory(new PrecisionModel(10000));
var noder = new SnapRoundingNoder(new PrecisionModel(10000)); // adjust PrecisionModel to your needs, must be fixed.
noder.ComputeNodes(lineStrings.Select(s => (ISegmentString)new NodedSegmentString(s.Coordinates, s)).ToList());
var noded = noder.GetNodedSubstrings();
polygonizer.Add(noded.Select(n => (Geometry)gf.CreateLineString(n.Coordinates)).ToList());
//for ( int i = 0; i < lineStrings.Count; i++)
//{
// polygonizer.Add(lineStrings[i]);
//}
ICollection<NetGeometry> result = polygonizer.GetPolygons();
foreach (NetGeometry index in result)
{
if ( index is Polygon pg)
{
polygons.Add(pg);
}
}
return polygons;
}
Results after application
All input geometries to Polygonizer should be LineStrings.
The whole input set of LineStrings must be fully noded:
var noder = new SnapRoundingNoder(new PrecisionModel(1000)); // adjust PrecisionModel to your needs, must be fixed.
noder.ComputeNodes(lines.Select(s => (ISegmentString)new NodedSegmentString(s.Coordinates, s)).ToList());
var noded = noder.GetNodedSubstrings();
polygonizer.Add(noded.Select(n => (Geometry)gf.CreateLineString(n.Coordinates)).ToList());
Related
I am trying to extract triangulated meshes of all geometries in an IFC file using this code
I have already loaded the model with ifcstore.open...
var context = new Xbim3DModelContext(model);
context.CreateContext();
//var geometries = context.ShapeGeometries();
//XbimShapeTriangulation mesh = null;
var geometries = context.ShapeInstances();
foreach (var g in geometries)
{
//var ms = new MemoryStream(((IXbimShapeGeometryData)g).ShapeData);
//var br = new BinaryReader(ms);
//mesh = br.ReadShapeTriangulation();
////mesh = mesh.Transform(((XbimShapeInstance)g).Transformation);
//Console.WriteLine(g.Format + " | " + g.ShapeLabel);
//Console.WriteLine(mesh.Faces.Count() + " | " + mesh.Vertices.Count());
var tri = context.ShapeGeometryMeshOf(g);
Console.WriteLine(tri.TriangleIndexCount + " | " + tri.ToString());
}
If I'm using the commented part of the above code, the mesh returns without being triangulated. The format is PolyHedronBinary.
If I use the context.ShapeGeometryMeshOf() method, there is an exception thrown : invalid geometry type .
Please help me with triangulating the geometries of the model.
I have also read about the method "read" in XbimWindowsUI/Xbim.Presentation/MeshGeometry3DExtensions.cs, but I am not able to figure out what I have to pass as the "m3d" parameter ?
/// <summary>
/// Reads a triangulated model from an array of bytes and adds the mesh
/// to the current state
/// </summary>
/// <param name="m3D"></param>
/// <param name="mesh">byte array of XbimGeometryType.PolyhedronBinary Data</param>
/// <param name="transform">Transforms the mesh to the new position if not null</param>
public static void Read(
this MeshGeometry3D m3D,
byte[] mesh,
XbimMatrix3D? transform = null)
It will be great if anybody could provide/ point me to example usage of this method.
I need to rebuild the IFC model in Unity and hence I need the triangulated mesh data.
Also suggest if there is anyway to achieve this more efficiently and/or in a simpler way!
I used some code from the xBIM GIT repository for this:
IfcStore model = IfcStore.Open(ifcFileName);
if (model.GeometryStore.IsEmpty)
{
var context = new Xbim3DModelContext(model);
context.CreateContext();
}
foreach (var ifcElement in model.Instances.OfType<IfcElement>())
{
XbimModelPositioningCollection modelPositions = new XbimModelPositioningCollection();
short userDefinedId = 0;
model.UserDefinedId = userDefinedId;
modelPositions.AddModel(model.ReferencingModel);
if (model.IsFederation)
{
foreach (var refModel in model.ReferencedModels)
{
refModel.Model.UserDefinedId = ++userDefinedId;
var v = refModel.Model as IfcStore;
if (v != null)
modelPositions.AddModel(v.ReferencingModel);
}
}
var modelBounds = modelPositions.GetEnvelopeInMeters();
var p = modelBounds.Centroid();
var modelTranslation = new XbimVector3D(-p.X, -p.Y, -p.Z);
var oneMeter = model.ModelFactors.OneMetre;
var translation = XbimMatrix3D.CreateTranslation(modelTranslation * oneMeter);
var scaling = XbimMatrix3D.CreateScale(1 / oneMeter);
var transform = translation * scaling;
var mat = GetStyleFromXbimModel(ifcElement);
var m = GetGeometry(ifcElement, transform, mat);
var myRetTuple = WriteTriangles(m);
}`
the WriteTriangle-Function:
private Tuple<Point3D> WriteTriangles(IXbimMeshGeometry3D wpfMeshGeometry3D)
{
var axesMeshBuilder = new MeshBuilder();
var pos = wpfMeshGeometry3D.Positions.ToArray();
var nor = wpfMeshGeometry3D.Normals.ToArray();
var areasum = 0.00;
for (var i = 0; i < wpfMeshGeometry3D.TriangleIndices.Count; i += 3)
{
var p1 = wpfMeshGeometry3D.TriangleIndices[i];
var p2 = wpfMeshGeometry3D.TriangleIndices[i + 1];
var p3 = wpfMeshGeometry3D.TriangleIndices[i + 2];
if (nor[p1] == nor[p2] && nor[p1] == nor[p3]) // same normals
{
var cnt = FindCentroid(new[] { pos[p1], pos[p2], pos[p3] });
CreateNormal(cnt, nor[p1], axesMeshBuilder);
}
else
{
CreateNormal(pos[p1], nor[p1], axesMeshBuilder);
CreateNormal(pos[p2], nor[p2], axesMeshBuilder);
CreateNormal(pos[p3], nor[p3], axesMeshBuilder);
}
var point1 = new Point3D(pos[p1].X, pos[p1].Y, pos[p1].Z);
var point2 = new Point3D(pos[p2].X, pos[p2].Y, pos[p2].Z);
var point3 = new Point3D(pos[p3].X, pos[p3].Y, pos[p3].Z);
}
return Tuple.Create(point1, point2, point3);
}
and some additional methods from xBIM GeometryHandler:
private static XbimPoint3D FindCentroid(XbimPoint3D[] p)
{
double x = 0;
double y = 0;
double z = 0;
var n = 0;
foreach (var item in p)
{
x += item.X;
y += item.Y;
z += item.Z;
n++;
}
if (n <= 0)
return new XbimPoint3D(x, y, z);
x /= n;
y /= n;
z /= n;
return new XbimPoint3D(x, y, z);
}
private static void CreateNormal(XbimPoint3D pnt, XbimVector3D vector3D, MeshBuilder axesMeshBuilder)
{
var cnt = new Point3D() { X = pnt.X, Y = pnt.Y, Z = pnt.Z };
var path = new List<Point3D> { cnt };
const double nrmRatio = .2;
path.Add(
new Point3D(
cnt.X + vector3D.X * nrmRatio,
cnt.Y + vector3D.Y * nrmRatio,
cnt.Z + vector3D.Z * nrmRatio
));
const double lineThickness = 0.001;
axesMeshBuilder.AddTube(path, lineThickness, 9, false);
}
private static WpfMeshGeometry3D GetGeometry(IPersistEntity selection, XbimMatrix3D modelTransform, Material mat)
{
var tgt = new WpfMeshGeometry3D(mat, mat);
tgt.BeginUpdate();
using (var geomstore = selection.Model.GeometryStore)
{
using (var geomReader = geomstore.BeginRead())
{
foreach (var shapeInstance in geomReader.ShapeInstancesOfEntity(selection).Where(x => x.RepresentationType == XbimGeometryRepresentationType.OpeningsAndAdditionsIncluded))
{
IXbimShapeGeometryData shapegeom = geomReader.ShapeGeometry(shapeInstance.ShapeGeometryLabel);
if (shapegeom.Format != (byte)XbimGeometryType.PolyhedronBinary)
continue;
var transform = shapeInstance.Transformation * modelTransform;
tgt.Add(
shapegeom.ShapeData,
shapeInstance.IfcTypeId,
shapeInstance.IfcProductLabel,
shapeInstance.InstanceLabel,
transform,
(short)selection.Model.UserDefinedId
);
}
}
}
tgt.EndUpdate();
return tgt;
}
private static DiffuseMaterial GetStyleFromXbimModel(IIfcProduct item, double opacity = 1)
{
var context = new Xbim3DModelContext(item.Model);
var productShape = context.ShapeInstancesOf(item)
.Where(s => s.RepresentationType != XbimGeometryRepresentationType.OpeningsAndAdditionsExcluded)
.ToList();
var wpfMaterial = GetWpfMaterial(item.Model, productShape.Count > 0 ? productShape[0].StyleLabel : 0);
var newmaterial = wpfMaterial.Clone();
((DiffuseMaterial)newmaterial).Brush.Opacity = opacity;
return newmaterial as DiffuseMaterial;
}
private static Material GetWpfMaterial(IModel model, int styleId)
{
var sStyle = model.Instances[styleId] as IIfcSurfaceStyle;
var wpfMaterial = new WpfMaterial();
if (sStyle != null)
{
var texture = XbimTexture.Create(sStyle);
texture.DefinedObjectId = styleId;
wpfMaterial.CreateMaterial(texture);
return wpfMaterial;
}
var defautMaterial = ModelDataProvider.DefaultMaterials;
Material material;
if (defautMaterial.TryGetValue(model.GetType().Name, out material))
{
return material;
}
var color = new XbimColour("red", 1, 1, 1);
wpfMaterial.CreateMaterial(color);
return wpfMaterial;
}
I'm writing a project about game's character data.
And each character in the data document have four types, Lv1 and LvMAX, and HP, STR, VIT, INT, MEN.
I use top one code at the middle part and got NullReferenceException when I use it to get some data like:
int x = CD.Parameters.Basic.Awaked.Force.Lv1.STR;
Force will be null. But when I use buttom one at the middle part, Force won't be null.
What's the difference between that two?
Code below
public class ParamType
{
public ParamLv Mebius, Force, Aegis, Magius;
string cost;
DataRow[] Datas;
List<int> ToMebius = new List<int>(), ToForce = new List<int>(), ToAegis = new List<int>(), ToMagius = new List<int>(); //HP, HP, STR, STR, VIT, VIT, INT, INT, MEN, MEN
public ParamType(SData Data, bool awaked)
{
if (awaked)
{
Data.CharaID = CharaCOM.AwakedID(Data.CharaID);
}
Datas = DataCOM.Search(Data.CharaID, Data.DTs.Source, Data.TitleP.Start[(int)DataTitle.CharacterParams], Const.COL_CHARACTER_ID, Const.COL_CHARACTER_ID);
cost = DataCOM.Search(Data.DTs.Source, Data.CharaID, Const.COL_COST, 0, Data.TitleP.Start[(int)DataTitle.CharacterParams], Const.COL_CHARACTER_ID_WITH_TYPE);
List<int>[] SArray = { ToMebius, ToForce, ToAegis, ToMagius };
for (int i = 0; i < Datas.Length; i++)
{
SArray[i] = new List<int>();
for (int j = Const.COL_PARAM_MIN; j < Const.COL_PARAM_MIN + Const.COL_PARAM_LENGTH; j++)
{
SArray[i].Add(Convert.ToInt32(Datas[i][j]));
}
}
/*
this will send NullReference Exception
ParamLv[] PLArray = new ParamLv[4];
for (int i = 0; i < SArray.Length; i++)
{
PLArray[i] = new ParamLv(Data, SArray[i]);
}
*/
/*
This won't get exception and I can get correct data I want.
Mebius = new ParamLv(Data, SArray[0]);
Force = new ParamLv(Data, SArray[1]);
Aegis = new ParamLv(Data, SArray[2]);
Magius = new ParamLv(Data, SArray[3]);
*/
}
public class ParamLv
{
public Params Lv1, LvMax;
List<int> ToLv1 = new List<int>(), ToLvMAX = new List<int>(); //HP, STR, VIT, INT, MEN
public ParamLv(SData Data, List<int> ParamsL)
{
for (int i = 0; i < ParamsL.Count; i += Const.COL_PARAM_MIN_MAX_GAP)
{
ToLv1.Add(ParamsL[i]);
ToLvMAX.Add(ParamsL[i + 1]);
}
Lv1 = new Params(Data, ToLv1);
LvMax = new Params(Data, ToLvMAX);
}
public class Params
{
//some method and properties to get or set Parameters.
}
}
Please tell me if something still bad, and this is my first time to ask question here, so If I did something wrong, please tell me. Thanks for #MicroVirus , #Moriarty and #mvikhona told my mistake.
Mebius = new ParamLv(Data, SArray[0]);
Force = new ParamLv(Data, SArray[1]);
Aegis = new ParamLv(Data, SArray[2]);
Magius = new ParamLv(Data, SArray[3]);
This works, because you are assigning reference to new ParamLv to your properties.
But in this case:
ParamLv[] PLArray = { Mebius, Force, Aegis, Magius };
for (int i = 0; i < PLArray.Length; i++)
{
PLArray[i] = new ParamLv(Data, SArray[i]);
}
you aren't filling your array with variables/properties themselves, but you are filling it with references your properties hold, in the end your array will hold reference to 4 new ParamLw, but your property Force will stay null.
Edit:
I'll try to explain it a bit different. Let's say you have this code:
ParamLv[] PLArray = { Force };
At this moment value of PLArray[0] is same as value of Force, but PLArray[0] isn't Force.
The moment you do this:
PLArray[0] = new ParamLv(Data, null);
new ParamLv(Data, null) returns reference to new ParamLv and you assign this to your PLArray[0], but like I said before PLArray[0] isn't Force, so Force will stay unchanged.
If that didn't explain it well, try to look at this piece of code, it does what you are trying to do.
int a = 1;
int[] myArr = { a }; // a = 1, myArr[0] = 1
myArr[0] = 2; // a = 1, myArr[0] = 2
object obj = null;
object[] objArr = { obj }; // obj = null, objArr[0] = null
objArr[0] = new object(); // obj = null, objArr[0] = 'reference to new object'
I have created a new Mleader style in autocad using C#. I want to use it in CAD; to assign Mleader style to a leader. I have no idea regarding this. I tried this code
public class test
{
public void drawMleaders(Transaction acTrans, Database acCurDb, Document acDoc, double scale, double gap, double[] pickPont)
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
//Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
DBDictionary mlStyles = (DBDictionary)acTrans.GetObject(acCurDb.MLeaderStyleDictionaryId, OpenMode.ForWrite);
// mlStyles.UpgradeOpen();
if (!mlStyles.Contains("MyLeaderStyle"))
{
MLeaderStyle dst = new MLeaderStyle();
// MText mt = new MText();
// mt.Contents = text;
//dst.Name="MyLeaderStyle";
dst.ArrowSymbolId = ObjectId.Null;
dst.ArrowSize = 0.5 * scale;
//dst.ContentType = 0;
//dst.DefaultMText = "";
dst.LandingGap = 0;
dst.EnableBlockRotation = true;
dst.MaxLeaderSegmentsPoints = 2;
dst.EnableLanding = true;
dst.PostMLeaderStyleToDb(acCurDb, "MyLeaderStyle");
acCurDb.MLeaderstyle = dst.ObjectId;
//dst1.Add(dst);
acTrans.AddNewlyCreatedDBObject(dst, true);
}
MLeader lead = new MLeader();
int i = lead.AddLeader();
lead.AddLeaderLine(i);
lead.AddFirstVertex(i, new Point3d(pickPont[0], pickPont[1], 0));
lead.AddLastVertex(i, new Point3d(pickPont[0] + 5, pickPont[1] + 5, 0));
//lead.MLeaderStyle = acCurDb.MLeaderstyle;
acBlkTblRec.AppendEntity(lead);
acTrans.AddNewlyCreatedDBObject(lead, true);
}
}
Any help would be appreciated.
You just have to set the leader.MLeaderStyle property to the MLeader style ObjectId.
You can get it from the MLeaderStyle dictionary if already exists or from the PostMLeaderStyleToDb return value if create it
ObjectId mlStyleId;
DBDictionary mlStyles = (DBDictionary)acTrans.GetObject(acCurDb.MLeaderStyleDictionaryId, OpenMode.ForRead);
if (mlStyles.Contains("MyLeaderStyle"))
{
mlStyleId = mlStyles.GetAt("MyLeaderStyle");
}
else
{
MLeaderStyle dst = new MLeaderStyle();
dst.ArrowSymbolId = ObjectId.Null;
dst.ArrowSize = 0.5 * scale;
dst.LandingGap = 0;
dst.EnableBlockRotation = true;
dst.MaxLeaderSegmentsPoints = 2;
dst.EnableLanding = true;
mlStyleId = dst.PostMLeaderStyleToDb(acCurDb, "MyLeaderStyle");
acTrans.AddNewlyCreatedDBObject(dst, true);
}
MLeader lead = new MLeader();
int i = lead.AddLeader();
lead.AddLeaderLine(i);
lead.AddFirstVertex(i, new Point3d(pickPont[0], pickPont[1], 0));
lead.AddLastVertex(i, new Point3d(pickPont[0] + 5, pickPont[1] + 5, 0));
lead.MLeaderStyle = mlStyleId;
acBlkTblRec.AppendEntity(lead);
acTrans.AddNewlyCreatedDBObject(lead, true);
acTrans.Commit();
I have an xml file that I am accessing to create a report of time spent on a project. I'm returning the unique dates to a label created dynamically on a winform and would like to compile the time spent on a project for each unique date. I have been able to return all of the projects under each date or only one project. Currently I'm stuck on only returning one project. Can anyone please help me?? This is what the data should look like if it's correct.
04/11/15
26820 2.25
27111 8.00
04/12/15
26820 8.00
04/13/15
01det 4.33
26820 1.33
27225 4.25
etc.
This is how I'm retrieving the data
string[] weekDateString = elementDateWeekstring();
string[] uniqueDates = null;
string[] weeklyJobNumber = elementJobNumWeek();
string[] weeklyTicks = elementTicksWeek();
This is how I'm getting the unique dates.
IEnumerable<string> distinctWeekDateIE = weekDateString.Distinct();
foreach (string d in distinctWeekDateIE)
{
uniqueDates = distinctWeekDateIE.ToArray();
}
And this is how I'm creating the labels.
try
{
int dateCount;
dateCount = uniqueDates.Length;
Label[] lblDate = new Label[dateCount];
int htDate = 1;
int padDate = 10;
for (int i = 0; i < dateCount; i++ )
{
lblDate[i] = new Label();
lblDate[i].Name = uniqueDates[i].Trim('\r');
lblDate[i].Text = uniqueDates[i];
lblDate[i].TabIndex = i;
lblDate[i].Bounds = new Rectangle(18, 275 + padDate + htDate, 75, 22);
targetForm.Controls.Add(lblDate[i]);
htDate += 22;
foreach (string x in uniqueDates)
{
int[] posJobNumber;
posJobNumber = weekDateString.Select((b, a) => b == uniqueDates[i].ToString() ? a : -1).Where(a => a != -1).ToArray();
for (int pjn = 0; pjn < posJobNumber.Length; pjn++)
{
if (x.Equals(lblDate[i].Text))
{
Label lblJobNum = new Label();
int htJobNum = 1;
int padJobNum = 10;
lblJobNum.Name = weeklyJobNumber[i];
lblJobNum.Text = weeklyJobNumber[i];
lblJobNum.Bounds = new Rectangle(100, 295 + padJobNum + htJobNum, 75, 22);
targetForm.Controls.Add(lblJobNum);
htJobNum += 22;
htDate += 22;
padJobNum += 22;
}
}
}
}
}
I've been stuck on this for about 3 months. Is there anyone that can describe to me why I'm not able to properly retrieve the job numbers that are associated with a particular date. I don't believe that these are specifically being returned as dates. Just a string that looks like a date.
I really appreciate any help I can get. I'm just completely baffled. Thank you for any responses in advance. I truly appreciate the assistance.
EDIT: #Sayka - Here is the xml sample.
<?xml version="1.0" encoding="utf-8"?>
<Form1>
<Name Key="4/21/2014 6:51:17 AM">
<Date>4/21/2014</Date>
<JobNum>26820</JobNum>
<RevNum>00000</RevNum>
<Task>Modeling Secondary</Task>
<Start>06:51 AM</Start>
<End>04:27 PM</End>
<TotalTime>345945089017</TotalTime>
</Name>
<Name Key="4/22/2014 5:44:22 AM">
<Date>4/22/2014</Date>
<JobNum>26820</JobNum>
<RevNum>00000</RevNum>
<Task>Modeling Secondary</Task>
<Start>05:44 AM</Start>
<End>06:56 AM</End>
<TotalTime>43514201221</TotalTime>
</Name>
<Name Key="4/22/2014 6:57:02 AM">
<Date>4/22/2014</Date>
<JobNum>02e-n-g</JobNum>
<RevNum>00000</RevNum>
<Task>NET Eng</Task>
<Start>06:57 AM</Start>
<End>07:16 AM</End>
<TotalTime>11706118875</TotalTime>
</Name>
....
</Form1>
This is how I'm getting the information out of the xml file and returning a string[].
public static string[] elementDateWeekstring()
{
//string datetxtWeek = "";
XmlDocument xmldoc = new XmlDocument();
fileExistsWeek(xmldoc);
XmlNodeList nodeDate = xmldoc.GetElementsByTagName("Date");
int countTicks = 0;
string[] dateTxtWeek = new string[nodeDate.Count];
for (int i = 0; i < nodeDate.Count; i++)
{
dateTxtWeek[i] = nodeDate[i].InnerText;
countTicks++;
}
return dateTxtWeek;
}
Job number and Ticks are returned in a similar fashion. I've been able to reuse these snippets throught out the code. This is a one dimensional xml file?? It will always return a position for a jobnumber that equates to a date or Ticks. I will never have more or less of any one element.
You can use Linq-to-XML to parse the XML file, and then use Linq-to-objects to group (and order) the data by job date and order each group by job name.
The code to parse the XML file is like so:
var doc = XDocument.Load(filename);
var jobs = doc.Descendants("Name");
// Extract the date, job number, and total time from each "Name" element.:
var data = jobs.Select(job => new
{
Date = (DateTime)job.Element("Date"),
Number = (string)job.Element("JobNum"),
Duration = TimeSpan.FromTicks((long)job.Element("TotalTime"))
});
The code to group and order the jobs by date and order the groups by job name is:
var result =
data.GroupBy(job => job.Date).OrderBy(g => g.Key)
.Select(g => new
{
Date = g.Key,
Jobs = g.OrderBy(item => item.Number)
});
Then you can access the data by iterating over each group in result and then iterate over each job in the group, like so:
foreach (var jobsOnDate in result)
{
Console.WriteLine("{0:d}", jobsOnDate.Date);
foreach (var job in jobsOnDate.Jobs)
Console.WriteLine(" {0} {1:hh\\:mm}", job.Number, job.Duration);
}
Putting this all together in a sample compilable console application (substitute the filename for the XML file as appropriate):
using System;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
private static void Main()
{
string filename = #"d:\test\test.xml"; // Substitute your own filename here.
// Open XML file and get a collection of each "Name" element.
var doc = XDocument.Load(filename);
var jobs = doc.Descendants("Name");
// Extract the date, job number, and total time from each "Name" element.:
var data = jobs.Select(job => new
{
Date = (DateTime)job.Element("Date"),
Number = (string)job.Element("JobNum"),
Duration = TimeSpan.FromTicks((long)job.Element("TotalTime"))
});
// Group the jobs by date, and order the groups by job name:
var result =
data.GroupBy(job => job.Date).OrderBy(g => g.Key)
.Select(g => new
{
Date = g.Key,
Jobs = g.OrderBy(item => item.Number)
});
// Print out the results:
foreach (var jobsOnDate in result)
{
Console.WriteLine("{0:d}", jobsOnDate.Date);
foreach (var job in jobsOnDate.Jobs)
Console.WriteLine(" {0} {1:hh\\:mm}", job.Number, job.Duration);
}
}
}
}
The output is like this
Create a new project
Set form size bigger.
Apply these codes.
Set the location for your XML file.
Namespaces
using System.Xml;
using System.IO;
Form Code
public partial class Form1 : Form
{
const string XML_FILE_NAME = "D:\\emps.txt";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
prepareDataGrid();
List<JOBS> jobsList = prepareXML(XML_FILE_NAME);
for (int i = 0; i < jobsList.Count; i++)
{
addDateRow(jobsList[i].jobDate.ToString("M'/'d'/'yyyy"));
for (int j = 0; j < jobsList[i].jobDetailsList.Count; j++)
dgv.Rows.Add(new string[] {
jobsList[i].jobDetailsList[j].JobNumber,
jobsList[i].jobDetailsList[j].JobHours
});
}
}
DataGridView dgv;
void prepareDataGrid()
{
dgv = new DataGridView();
dgv.BackgroundColor = Color.White;
dgv.GridColor = Color.White;
dgv.DefaultCellStyle.SelectionBackColor = Color.White;
dgv.DefaultCellStyle.SelectionForeColor = Color.Black;
dgv.DefaultCellStyle.ForeColor = Color.Black;
dgv.DefaultCellStyle.BackColor = Color.White;
dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dgv.Width = 600;
dgv.Dock = DockStyle.Left;
this.BackColor = Color.White;
dgv.Columns.Add("Col1", "Col1");
dgv.Columns.Add("Col2", "Col2");
dgv.Columns[0].Width = 110;
dgv.Columns[1].Width = 40;
dgv.DefaultCellStyle.Font = new System.Drawing.Font("Segoe UI", 10);
dgv.RowHeadersVisible = dgv.ColumnHeadersVisible = false;
dgv.AllowUserToAddRows =
dgv.AllowUserToDeleteRows =
dgv.AllowUserToOrderColumns =
dgv.AllowUserToResizeColumns =
dgv.AllowUserToResizeRows =
!(dgv.ReadOnly = true);
Controls.Add(dgv);
}
void addJobRow(string jobNum, string jobHours)
{
dgv.Rows.Add(new string[] {jobNum, jobHours });
}
void addDateRow(string date)
{
dgv.Rows.Add(new string[] { date, ""});
dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.SelectionForeColor =
dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.ForeColor = Color.Firebrick;
dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.Font = new Font("Segoe UI Light", 13.5F);
dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
dgv.Rows[dgv.Rows.Count - 1].Height = 25;
}
List<JOBS> prepareXML(string fileName)
{
string xmlContent = "";
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(fs)) xmlContent = sr.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlContent);
List<JOBS> jobsList = new List<JOBS>();
XmlNode form1Node = doc.ChildNodes[1];
for (int i = 0; i < form1Node.ChildNodes.Count; i++)
{
XmlNode dateNode = form1Node.ChildNodes[i].ChildNodes[0].ChildNodes[0],
jobNumNode = form1Node.ChildNodes[i].ChildNodes[1].ChildNodes[0],
timeTicksNode = form1Node.ChildNodes[i].ChildNodes[6].ChildNodes[0];
bool foundDate = false;
for (int j = 0; j < jobsList.Count; j++) if (jobsList[j].compareDate(dateNode.Value))
{
jobsList[j].addJob(jobNumNode.Value, Math.Round(TimeSpan.FromTicks(
(long)Convert.ToDouble(timeTicksNode.Value)).TotalHours, 2).ToString());
foundDate = true;
break;
}
if (!foundDate)
{
JOBS job = new JOBS(dateNode.Value);
string jbnum = jobNumNode.Value;
string tbtck = timeTicksNode.Value;
long tktk = Convert.ToInt64(tbtck);
double tkdb = TimeSpan.FromTicks(tktk).TotalHours;
job.addJob(jobNumNode.Value, Math.Round(TimeSpan.FromTicks(
Convert.ToInt64(timeTicksNode.Value)).TotalHours, 2).ToString());
jobsList.Add(job);
}
}
jobsList.OrderByDescending(x => x.jobDate);
return jobsList;
}
class JOBS
{
public DateTime jobDate;
public List<JobDetails> jobDetailsList = new List<JobDetails>();
public void addJob(string jobNumber, string jobHours)
{
jobDetailsList.Add(new JobDetails() { JobHours = jobHours, JobNumber = jobNumber });
}
public JOBS(string dateString)
{
jobDate = getDateFromString(dateString);
}
public JOBS() { }
public bool compareDate(string dateString)
{
return getDateFromString(dateString) == jobDate;
}
private DateTime getDateFromString(string dateString)
{
string[] vals = dateString.Split('/');
return new DateTime(Convert.ToInt32(vals[2]), Convert.ToInt32(vals[0]), Convert.ToInt32(vals[1]));
}
}
class JobDetails
{
public string JobNumber { get; set; }
public string JobHours { get; set; }
}
}
Below is my main list
var serie_line = new { name = series_name, data = new List<object>() };
here I add items in data as follows,
serie_line.data.Add(child_object_name);
serie_line.data.Add(period_final_value );
I then add this var serie_line to another list series as follows,
List<object> series = new List<object>();
series.Add(serie_line);
finally ,I serialize this series into JSON as below,
var obj4 = new { legend = legend, title,chart, series};
JSON_File_Returned = jSearializer.Serialize(obj4);
whereas
System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer();
Now Output I am getting is as follows,
{
"legend":{"enabled":"true"},
"title":{"text":"Financial"},
"chart":{"type":"pie"},
"series":[
{"name":"Actual","data":["Market Share",20.00]},
{"name":"Actual","data":["Sales Growth",30.00]},
{"name":"Actual","data":["Operating Profit",40.00]},
{"name":"Actual","data":["Gross Margin %",10.00]}
]
}
But my required output is as follows,
{
"legend":{"enabled":"true"},
"title":{"text":"Financial"},
"chart":{"type":"pie"},
"series":[
{"name":"Actual","data":[["Market Share",20.00],["Sales Growth",30.00],["Operating Profit",40.00],["Gross Margin %",10.00]]}
]
}
So..That I can plot pie chart in highchart using this JSON output...I have tried for everything like using dictionary,making different class and then using it's object and so on...but can't make it out....
Below is my entire code...if in case I am messing with any loop and I don't recognize it but any one might notice it..please check the below code for the same..
var serie_line = new { name = series_name, data = new List<object>() };
for (int k = 0; k <= read_Series_Splitted_Name.Length; k++) //for loop for passing chart type in series
{
for (int i = 0; i < id_series_before_offset.Count; i++) //for loop for counting series ID
{
var xmlAttributeCollection = id_series_before_offset[i].Attributes;
if (xmlAttributeCollection != null)
{
var seriesid = xmlAttributeCollection["id"];
xmlActions_id[i] = seriesid.Value;
resulted_series_id = seriesid.Value;
series_name = Client.GetAttributeAsString(sessionId, resulted_series_id, "name", "");
new_series_name = series_name;
series_Atribute = Client.GetAttributeAsString(sessionId, resulted_series_id, "symbol", "");
if (read_Series_Splitted_Name_store == series_Atribute)
{
serie_line = new { name = series_name, data = new List<object>() };
}
k++;
// Forloop for PeriodId and It's Value
var value = Read_XML_Before_Offset.SelectNodes("//measure.values/series[" + (i + 1) + "]/value");
var xmlActions = new string[value.Count];// for periodname
var xmlActionsone = new string[value.Count]; // for period value
for (int j = 0; j < value.Count; j++)
{
var xmlAttributeCollection_for_period = value[j].Attributes;
if (xmlAttributeCollection_for_period != null)
{
if (i == 0 && a == 0)
{
var periodid = xmlAttributeCollection_for_period["periodid"];
xmlActions[j] = periodid.Value;
period_final_id = periodid.Value;
period_name = Client.GetAttributeAsString(sessionId, periodid.Value, "name", "");
period_Name.Add(period_name);
}
try
{
var action = xmlAttributeCollection_for_period["value"]; // xmlActionsone[j] = action.Value;
period_final_value = float.Parse(action.Value);
// serie_line.data.Add(period_final_value);
serie_line.data.Add(child_object_name);
serie_line.data.Add(period_final_value );
}
catch (Exception ex1)
{
serie_line.data.Add("");
serie_line.data.Add( null );
}
}
}
}
}
}
series.Add(serie_line);
Your C# code should look something like this all stripped down:
var serie_line = new { name = "Actual", data = new List<object>() };
serie_line.data.Add(new List<object>() {"Market Share", 20.0});
serie_line.data.Add(new List<object>() {"Sales Growth", 30.0});
serie_line.data.Add(new List<object>() {"Operting Profit", 40.0});
serie_line.data.Add(new List<object>() {"Gross Margin %", 10.0});
jSearializer.Serialize(serie_line);
Which produces:
{"name":"Actual","data":[["Market Share",20],["Sales Growth",30],["Operting Profit",40],["Gross Margin %",10]]}
I'm not following the bottom part of the code (how you create child_object_name and period_final_value, but I think you want:
serie_line.data.Add(new List<object>() {child_object_name, period_final_value });
Instead of:
serie_line.data.Add(child_object_name);
serie_line.data.Add(period_final_value );