Mleader style in Autocad using C# - c#

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();

Related

CSharp Generate polygons from a set of intersecting lines

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());

working Faster-rcnn in cntk c#?

i'm trying to get faster-rcnn model working in C# code.
i have a faster-rcnn trained model that builds and tests in the CNTK python code Below is my attempts to get it working from c#
so far i have:
string testImage = #"C:\data\images\mytestimage.jpg";
Bitmap bitmap = new Bitmap(Bitmap.FromFile(testImage ));
DeviceDescriptor device = DeviceDescriptor.CPUDevice;
Function modelFunc = Function.Load(modelPath, device);
var inDoims = modelFunc.Arguments;
Variable inputVar = modelFunc.Arguments.FirstOrDefault();
Variable inputVar2 = modelFunc.Arguments[1];
NDShape inputShape = inputVar.Shape;
int imageWidth = inputShape[0];
int imageHeight = inputShape[1];
int imageChannels = inputShape[2];
int imageSize = inputShape.TotalSize;
bitmap = ImageProcessing.Resize(bitmap, imageWidth, imageHeight, true);
var pixels = ImageProcessing.ParallelExtractCHW(bitmap);
Variable input = modelFunc.Arguments[0];
var inputDataMap = new Dictionary<Variable, Value>();
List<float> input2Vals = new List<float>();
input2Vals.Add(imageWidth);
input2Vals.Add(imageHeight);
input2Vals.Add(imageWidth);
input2Vals.Add(imageHeight);
input2Vals.Add(imageWidth);
input2Vals.Add(imageHeight);
Value inputVal = Value.CreateBatch(inputVar.Shape, pixels, device);
Value inputValue2 = Value.CreateBatch(inputVar2.Shape, input2Vals, device);
inputDataMap[input] = inputVal;
inputDataMap[inputVar2] = inputValue2;
NDShape outputShape1 = modelFunc.Outputs[0].Shape;
NDShape outputShape2 = modelFunc.Outputs[1].Shape;
NDShape outputShape3 = modelFunc.Outputs[2].Shape;
Value outputValue1 = null;
Value outputValue2 = null; ;
Value outputValue3 = null; ;
var outputDataMap = new Dictionary<Variable, Value>()
{
{ modelFunc.Outputs[0], outputValue1 },
{ modelFunc.Outputs[1], outputValue2 },
{ modelFunc.Outputs[2], outputValue3 }
};
//run the model
modelFunc.Evaluate(inputDataMap, outputDataMap, device);
var out0 = outputDataMap[modelFunc.Outputs[0]];
var out1 = outputDataMap[modelFunc.Outputs[1]];
var out2 = outputDataMap[modelFunc.Outputs[2]];
var clsPred = out0.GetDenseData<float>(modelFunc.Outputs[0])[0];
var rois = out1.GetDenseData<float>(modelFunc.Outputs[1])[0];
var vbboxR = out2.GetDenseData<float>(modelFunc.Outputs[2])[0];
var labels = new[] { "__background__", "firstobject", "secondobject", "thirdsobject"};
....
}
but the results I get back in clsPred don't make any sense...for example when my test image is completely blank...the model seems to think there are objects in it....
Has anyone any recommendations?
Buzz

cannot extract triangulated geometry from IFC using xBIM

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;
}

Changing color of a line serie in EPPlus

Is there an easier way to change the color of a line serie?
I tried using this. But the serieNode is producing a NullReferenceException at serieNode.AppendChild(spPr);.
Here's the code that generates the graph:
private void GenerateLicenseUsageStatsChart(FileInfo excelFileInfo, FileInfo
csvFileInfo, DateTime lastCheckedDate)
{
string worksheetsName = "Sheet1";
const bool firstRowIsHeader = false;
var excelTextFormat = new ExcelTextFormat { Delimiter = ',' };
// excelTextFormat.EOL = "\r";
using (ExcelPackage package = new ExcelPackage(excelFileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(csvFileInfo, excelTextFormat,
OfficeOpenXml.Table.TableStyles.None, firstRowIsHeader);
var workbook = package.Workbook;
ExcelWorksheet workSheet = workbook.Worksheets[1];
var chart = workSheet.Drawings.AddChart("chart",
eChartType.ColumnClustered);
chart.SetPosition(10, 250);
chart.SetSize(700, 500);
chart.Title.Text = $"LicenseUsageStats {lastCheckedDate:MMM}
{lastCheckedDate.Year}";
chart.YAxis.MajorUnit = 1;
chart.YAxis.MinorUnit = 1;
chart.XAxis.MajorTickMark = eAxisTickMark.None;
chart.XAxis.MinorTickMark = eAxisTickMark.None;
chart.XAxis.Title.Text = "Hour";
chart.XAxis.Title.Font.Size = 10;
var maxLicensesSerie = chart.Series.Add("B2:B25", "A2:A25");
maxLicensesSerie.Header = "Max Licenses Used";
var avgLicensesSerie = chart.Series.Add("C2:C25", "A2: A25");
avgLicensesSerie.Header = "Avg Licenses Used";
var maxLineChart = (ExcelLineChart)
chart.PlotArea.ChartTypes.Add(eChartType.Line);
var maxThreshLineSerie = maxLineChart.Series.Add("D2:D25",
"A2:A25");
maxThreshLineSerie.Header = "Max";
SetLineChartColor(maxLineChart, 0, Color.Red);
var warningLineChart =
(ExcelLineChart)chart.PlotArea.ChartTypes.Add(eChartType.Line);
var warningThreshLineSerie =
warningLineChart.Series.Add("E2:E25", "A2:A25");
warningThreshLineSerie.Header = "Warning";
SetLineChartColor(warningLineChart, 1, Color.Yellow);
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
// workSheet.Column(4).Hidden = true;
// workSheet.Column(5).Hidden = true;
package.Save();
}
}
My end goal:
Since excel doesn't have a neat way of creating horizontal lines, I created the max and warning columns. I would like to hide those columns as well, without them affecting the graph. Maybe I could "hide" them by moving them to another sheet?
I see the problem. The function assumes that the index of the serie matches the number of series in the chart object collection. But since it is a mixed chart that is not the case after casting the result of the Add. This is a bit of a hack but it will work (I really should think a little harder about how to match up the numbers):
public static void SetLineChartColor(this ExcelChart chart, int serieIdx, int chartSeriesIndex, Color color)
{
var chartXml = chart.ChartXml;
var nsa = chart.WorkSheet.Drawings.NameSpaceManager.LookupNamespace("a");
var nsuri = chartXml.DocumentElement.NamespaceURI;
var nsm = new XmlNamespaceManager(chartXml.NameTable);
nsm.AddNamespace("a", nsa);
nsm.AddNamespace("c", nsuri);
var serieNode = chart.ChartXml.SelectSingleNode($#"c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser[c:idx[#val='{serieIdx}']]", nsm);
var serie = chart.Series[chartSeriesIndex];
var points = serie.Series.Length;
//Add reference to the color for the legend
var srgbClr = chartXml.CreateNode(XmlNodeType.Element, "srgbClr", nsa);
var att = chartXml.CreateAttribute("val");
att.Value = $"{color.R:X2}{color.G:X2}{color.B:X2}";
srgbClr.Attributes.Append(att);
var solidFill = chartXml.CreateNode(XmlNodeType.Element, "solidFill", nsa);
solidFill.AppendChild(srgbClr);
var ln = chartXml.CreateNode(XmlNodeType.Element, "ln", nsa);
ln.AppendChild(solidFill);
var spPr = chartXml.CreateNode(XmlNodeType.Element, "spPr", nsuri);
spPr.AppendChild(ln);
serieNode.AppendChild(spPr);
}
And you can call it like this:
maxLineChart.SetLineChartColor(2, 0, Color.Red);
warningLineChart.SetLineChartColor(3, 0, Color.Yellow);

How would you simplify this mapping? Array to objects

What would you consider to be the cleanest way to loop through img.Group.Contents and write out the values to the galleryImage.??? objects?
galleryImage.TinyImage = new myModel.Image._Img();
galleryImage.TinyImage.Url = img.Group.Contents[0].Url;
galleryImage.TinyImage.FileSize = img.Group.Contents[0].FileSize;
galleryImage.TinyImage.Type = img.Group.Contents[0].Type;
galleryImage.TinyImage.Medium = img.Group.Contents[0].Medium;
galleryImage.TinyImage.Width = img.Group.Contents[0].Width;
galleryImage.TinyImage.Height = img.Group.Contents[0].Height;
galleryImage.TinyImage.Hash = img.Group.Contents[0].Hash;
galleryImage.Thumbnail.Url = img.Group.Contents[1].Url;
galleryImage.Thumbnail.FileSize = img.Group.Contents[1].FileSize;
galleryImage.Thumbnail.Type = img.Group.Contents[1].Type;
galleryImage.Thumbnail.Medium = img.Group.Contents[1].Medium;
galleryImage.Thumbnail.Width = img.Group.Contents[1].Width;
galleryImage.Thumbnail.Height = img.Group.Contents[1].Height;
galleryImage.Thumbnail.Hash = img.Group.Contents[1].Hash;
galleryImage.SmallImage.Url = img.Group.Contents[2].Url;
galleryImage.SmallImage.FileSize = img.Group.Contents[2].FileSize;
galleryImage.SmallImage.Type = img.Group.Contents[2].Type;
galleryImage.SmallImage.Medium = img.Group.Contents[2].Medium;
galleryImage.SmallImage.Width = img.Group.Contents[2].Width;
galleryImage.SmallImage.Height = img.Group.Contents[2].Height;
galleryImage.SmallImage.Hash = img.Group.Contents[2].Hash;
galleryImage.MediumImage.Url = img.Group.Contents[3].Url;
galleryImage.MediumImage.FileSize = img.Group.Contents[3].FileSize;
galleryImage.MediumImage.Type = img.Group.Contents[3].Type;
galleryImage.MediumImage.Medium = img.Group.Contents[3].Medium;
galleryImage.MediumImage.Width = img.Group.Contents[3].Width;
galleryImage.MediumImage.Height = img.Group.Contents[3].Height;
galleryImage.MediumImage.Hash = img.Group.Contents[3].Hash;
galleryImage.LargeImage.Url = img.Group.Contents[4].Url;
galleryImage.LargeImage.FileSize = img.Group.Contents[4].FileSize;
galleryImage.LargeImage.Type = img.Group.Contents[4].Type;
galleryImage.LargeImage.Medium = img.Group.Contents[4].Medium;
galleryImage.LargeImage.Width = img.Group.Contents[4].Width;
galleryImage.LargeImage.Height = img.Group.Contents[4].Height;
galleryImage.LargeImage.Hash = img.Group.Contents[4].Hash;
galleryImage.ExtraLargeImage.Url = img.Group.Contents[5].Url;
galleryImage.ExtraLargeImage.FileSize = img.Group.Contents[5].FileSize;
galleryImage.ExtraLargeImage.Type = img.Group.Contents[5].Type;
galleryImage.ExtraLargeImage.Medium = img.Group.Contents[5].Medium;
galleryImage.ExtraLargeImage.Width = img.Group.Contents[5].Width;
galleryImage.ExtraLargeImage.Height = img.Group.Contents[5].Height;
galleryImage.ExtraLargeImage.Hash = img.Group.Contents[5].Hash;
Functions provide a nice way to simplify repeated tasks:
void ConfigureImage(MyImageType img, int pos) {
img.Url = img.Group.Contents[pos].Url;
img.FileSize = img.Group.Contents[pos].FileSize;
img.Type = img.Group.Contents[pos].Type;
img.Medium = img.Group.Contents[pos].Medium;
img.Width = img.Group.Contents[pos].Width;
img.Height = img.Group.Contents[pos].Height;
img.Hash = img.Group.Contents[pos].Hash;
}
With this function in hand, rewrite your code in only six lines:
ConfigureImage(galleryImage.TinyImage, 0);
ConfigureImage(galleryImage.Thumbnail, 1);
ConfigureImage(galleryImage.SmallImage, 2);
ConfigureImage(galleryImage.MediumImage, 3);
ConfigureImage(galleryImage.LargeImage, 4);
ConfigureImage(galleryImage.ExtraLargeImage, 5);
I assume that the galleryImage.??? objects are all the same type?
If so, declare an array for them:
var list = new [] {
galleryImage.TinyImage, galleryImage.Thumbnail, galleryImage.SmallImage,
galleryImage.MediumImage, galleryImage.LargeImage,
galleryImage.ExtraLargeImage };
Then you can loop through them with a for-loop:
for (int i=0; i<6; i++) {
list[i].Url = img.Group.Contents[i].Url;
list[i].FileSize = img.Group.Contents[i].FileSize;
list[i].Type = img.Group.Contents[i].Type;
list[i].Medium = img.Group.Contents[i].Medium;
list[i].Width = img.Group.Contents[i].Width;
list[i].Height = img.Group.Contents[i].Height;
list[i].Hash = img.Group.Contents[i].Hash;
}
Probably something like
int ix = 0;
foreach( var dst in new [] { galleryImage.TinyImage, galleryImage.Thumbnail, etc }) {
src = img.Group.Contents[ix];
dst.Url = src.Url;
dst.FileSize = src.FileSize;
dst.Type = src.Type;
dst.Medium = src.Medium;
dst.Width = src.Width;
dst.Height = src.Height;
dst.Hash = src.Hash;
ix++;
}
Based on #Blorgbeard's answer, here's my final code. I had to modify it a bit because the imgType array was going to be an unknown length. If I used a for loop, it would throw an "Index Out of Bounds Exception".
var imgType = new[] {
galleryImage.TinyImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.Thumbnail = new SmugMugGalleryModel.Image._Img(),
galleryImage.SmallImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.MediumImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.LargeImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.ExtraLargeImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.TwoExtraLargeImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.ThreeExtraLargeImage = new SmugMugGalleryModel.Image._Img(),
galleryImage.OriginalImage = new SmugMugGalleryModel.Image._Img(),
};
var count = 0;
foreach (var i in img.Group.Contents)
{
imgType[count].Url = i.Url;
imgType[count].FileSize = i.FileSize;
imgType[count].Type = i.Type;
imgType[count].Medium = i.Medium;
imgType[count].Width = i.Width;
imgType[count].Height = i.Height;
imgType[count].Hash = i.Hash;
count++;
}

Categories