/// <summary> /// Add a DXF entity to the document for an element. /// </summary> public override void TryAddDxfEntity(DxfFile document, T element, DxfRenderContext context) { // TODO: handle context / drawing range / etc. if (element.Representation == null) { return; } var entities = element.GetEntitiesFromRepresentation(); if (element.IsElementDefinition) { var block = new DxfBlock { Name = element.GetBlockName(), BasePoint = new DxfPoint(0, 0, 0) }; foreach (var e in entities) { block.Entities.Add(e); } document.Blocks.Add(block); document.BlockRecords.Add(new DxfBlockRecord(block.Name)); return; } foreach (var e in entities) { document.Entities.Add(e); } AddElementToLayer(document, element, entities, context); }
/// <summary> /// Renders the model in dxf to the returned stream. /// </summary> /// <param name="model">The model to render</param> public Stream Render(Model model) { var doc = new netDxf.DxfDocument(netDxf.Header.DxfVersion.AutoCad2018); var context = new DxfRenderContext(); context.Model = model; foreach (var element in model.Elements.Values) { if (!_dxfCreators.TryGetValue(element.GetType(), out var converter)) { continue; } if (converter.TryToCreateDxfEntity(element, context, out var entity)) { doc.AddEntity(entity); } } var stream = new MemoryStream(); doc.Save(stream); return(stream); }
public bool TryToCreateDxfEntity(Element element, DxfRenderContext context, out EntityObject perimeter) { var floor = element as Floor; perimeter = floor.Profile.Perimeter.ToDxf(); perimeter.Lineweight = netDxf.Lineweight.W20; return(true); }
private Symbol pickSymbolByContext(ContentElement contentElement, DxfRenderContext context) { var contentTransform = contentElement.Transform; var drawingRangeTransform = context.DrawingRange?.Transform ?? new Transform(); //TODO — pick an appropriate symbol based on the context orientation var symbol = contentElement?.Symbols?.FirstOrDefault(s => s.CameraPosition == SymbolCameraPosition.Top); return(symbol); }
private Symbol generateSymbolFromBoundingBox(ContentElement contentElement, DxfRenderContext context) { var bbox = contentElement.BoundingBox; var min = bbox.Min; var max = bbox.Max; var polygon = new Polygon((min.X, min.Y), (max.X, min.Y), (max.X, max.Y), (min.X, max.Y)); // TODO: handle context orientation return(new Symbol(new GeometryReference(null, new List <object> { polygon }), SymbolCameraPosition.Top)); }
/// <summary> /// Add a DXF entity to the document for an element instance. /// </summary> public override void TryAddDxfEntity(DxfFile document, ElementInstance elementInstance, DxfRenderContext context) { var insert = new DxfInsert { Location = elementInstance.Transform.ToDxfPoint(context), Rotation = elementInstance.Transform.ToDxfAngle(context), Name = elementInstance.BaseDefinition.GetBlockName() }; // some blocks may not get created, due to missing symbols. Only // insert if we find the block. if (document.Blocks.Any(b => b.Name == insert.Name)) { document.Entities.Add(insert); AddElementToLayer(document, elementInstance.BaseDefinition, new[] { insert }, context); } }
/// <summary> /// Create a new instance of the renderer. /// </summary> public ModelToDxf() { context = new DxfRenderContext(); }
/// <summary> /// Add the entities associated with a given element to the appropriate /// layer, adding a new layer if necessary. /// </summary> public void AddElementToLayer(DxfFile document, Element e, IEnumerable <DxfEntity> entities, DxfRenderContext context) { try { var config = context.MappingConfiguration; if (config == null) { // TODO: add a default configuration return; } var layerConfigForElement = FindLayerForElement(config, e); if (layerConfigForElement == null) { return; } var matchingLayer = document.Layers.FirstOrDefault((l) => l.Name == layerConfigForElement.LayerName); if (matchingLayer == null) { var layer = new DxfLayer(layerConfigForElement.LayerName, layerConfigForElement.LayerColor.ToDxfColor()); if (layerConfigForElement.Lineweight != 0) { // lineweight value is in 1/100ths of a millimeter layer.LineWeight = new DxfLineWeight { Value = (short)layerConfigForElement.Lineweight }; } document.Layers.Add(layer); matchingLayer = layer; } foreach (var entity in entities) { entity.Layer = matchingLayer.Name; if (layerConfigForElement.ElementColorSetting == MappingConfiguration.ElementColorSetting.TryGetColorFromMaterial) { if (e is GeometricElement ge) { var materialColor = ge.Material.Color; var entityColor = materialColor.ToDxfColor(); entity.Color = entityColor; entity.Color24Bit = materialColor.To24BitColor(); } } } } catch (Exception ex) { // TODO: Implement exception logging Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
/// <summary> /// Add a DXF entity to the document for an element. /// </summary> public void TryAddDxfEntity(DxfFile document, Element element, DxfRenderContext context) { this.TryAddDxfEntity(document, element as T, context); }
/// <summary> /// Add a DXF entity to the document for an element. /// </summary> public abstract void TryAddDxfEntity(DxfFile document, T element, DxfRenderContext context);
/// <summary> /// Create a DXF Entity Object for a ContentElement. /// </summary> public override void TryAddDxfEntity(DxfFile doc, ContentElement contentElement, DxfRenderContext context) { try { var chosenSymbol = pickSymbolByContext(contentElement, context); if (chosenSymbol == null) { Console.WriteLine($"Symbol for {contentElement.Id} was null"); chosenSymbol = generateSymbolFromBoundingBox(contentElement, context); return; } // TODO: make all this handle await? var geometry = chosenSymbol.GetGeometryAsync().GetAwaiter().GetResult(); if (geometry == null) { Console.WriteLine($"Failed to get geometry for {contentElement.Id}"); return; } var polygons = geometry.OfType <Polygon>().Select(p => p.ToDxf()).Where(e => e != null).ToList(); var polylines = geometry.OfType <Polyline>().Select(p => p.ToDxf()).Where(e => e != null).ToList(); var entities = new List <DxfEntity>(polygons.Union(polylines)); if (entities.Count() == 0) { Console.WriteLine($"No entities for {contentElement.Id}"); return; } var blockName = contentElement.GetBlockName(); var block = new DxfBlock { BasePoint = contentElement.Transform.ToDxfPoint(context), Name = blockName }; doc.BlockRecords.Add(new DxfBlockRecord(blockName)); foreach (var p in entities) { block.Entities.Add(p); } AddElementToLayer(doc, contentElement, entities, context); doc.Blocks.Add(block); // if it's not being used as an element definition, // add an instance of it to the drawing. if (!contentElement.IsElementDefinition) { var insert = new DxfInsert { Name = blockName, Location = contentElement.Transform.ToDxfPoint(context), }; doc.Entities.Add(insert); AddElementToLayer(doc, contentElement, new[] { insert }, context); } } catch (Exception e) { //TODO: implement exception logging Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }