private BoundingBoxXYZ GetBoundingBoxOfLevel(IList <Level> strLevels, int highFloorLevelInd, View3D view)
        {
            Level highFloorLevel = strLevels[highFloorLevelInd];
            Level lowFloorLevel  = strLevels[highFloorLevelInd - 1];

            // Get the outline of the entire 3D model
            FilteredElementCollector fec = new FilteredElementCollector(_doc).OfClass(typeof(Wall));
            BoundingBoxXYZ           ibb = fec.Cast <Wall>().First().get_BoundingBox(view);
            Outline outline = new Outline(ibb.Min, ibb.Max);

            foreach (Wall wall in fec)
            {
                BoundingBoxXYZ boxXYZ = wall.get_BoundingBox(view);
                outline.AddPoint(boxXYZ.Min);
                outline.AddPoint(boxXYZ.Max);
            }

            // Create a new BoundingBoxXYZ to define a 3D rectangular space
            BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ();

            if (highFloorLevelInd == 1)
            {
                // Set the lower left botton corner of the box
                // To show the fondation, use the Z of the current level - BOTTOMOFFSET
                boundingBoxXYZ.Min = new XYZ(
                    outline.MinimumPoint.X - SECTION_BOX_XY_MARGIN,
                    outline.MinimumPoint.Y - SECTION_BOX_XY_MARGIN,
                    lowFloorLevel.ProjectElevation - BOTTOM_OFFSET);
            }
            else
            {
                // Use the Z of the current level.
                boundingBoxXYZ.Min = new XYZ(
                    outline.MinimumPoint.X - SECTION_BOX_XY_MARGIN,
                    outline.MinimumPoint.Y - SECTION_BOX_XY_MARGIN,
                    lowFloorLevel.ProjectElevation);
            }

            boundingBoxXYZ.Max = new XYZ(
                outline.MaximumPoint.X + SECTION_BOX_XY_MARGIN,
                outline.MaximumPoint.Y + SECTION_BOX_XY_MARGIN,
                highFloorLevel.ProjectElevation);

            return(boundingBoxXYZ);
        }
Exemple #2
0
        private bool FinalOutlinContainseRefOutline(
            Wall toCheckdWall, Element originalSupporter, List <Element> loopSupporterList,
            out List <Element> remainList)
        {
            // Get outline of the ref volumn of the selected wall
            BoundingBoxXYZ ibb        = toCheckdWall.get_BoundingBox(_auxiliary3DView);
            double         offset     = UnitUtils.Convert(20, DisplayUnitType.DUT_CENTIMETERS, DisplayUnitType.DUT_DECIMAL_FEET);
            XYZ            newMin     = new XYZ(ibb.Min.X, ibb.Min.Y, ibb.Min.Z - DISTENCE_CONTINUOUS_SUPPORTER);
            XYZ            newMax     = new XYZ(ibb.Max.X, ibb.Max.Y, ibb.Min.Z);
            Outline        refOutline = new Outline(newMin, newMax);

            BoundingBoxXYZ originalSupporterBB      = originalSupporter.get_BoundingBox(_auxiliary3DView);
            Outline        orignialSupporterOutline = new Outline(originalSupporterBB.Min, originalSupporterBB.Max);
            Outline        finalOutline             = orignialSupporterOutline;

            remainList = new List <Element>();

            foreach (Element supporter in loopSupporterList)
            {
                BoundingBoxXYZ bb = supporter.get_BoundingBox(_auxiliary3DView);
                Outline        supporterOutline = new Outline(bb.Min, bb.Max);

                if (finalOutline.Intersects(supporterOutline, DISTENCE_CONTINUOUS_SUPPORTER))
                {
                    if (!supporterOutline.Equals(finalOutline))
                    {
                        finalOutline.AddPoint(bb.Min);
                        finalOutline.AddPoint(bb.Max);
                    }
                }
                else
                {
                    remainList.Add(supporter);
                }
            }

            return(finalOutline.ContainsOtherOutline(refOutline, TOLERENCE_CONTAINED_BY_SUPPORTER));
        }
        public static RenderData DrawMesh(Elements.Geometry.Mesh mesh, ref Outline outline, DisplayStyle displayStyle)
        {
            var min = new XYZ(double.MaxValue, double.MaxValue, double.MaxValue);
            var max = new XYZ(double.MinValue, double.MinValue, double.MinValue);

            var numVertices   = mesh.Triangles.Count * 3;
            var numPrimitives = mesh.Triangles.Count;
            var pType         = PrimitiveType.TriangleList;
            var numIndices    = GetPrimitiveSize(pType) * numPrimitives;
            VertexFormatBits vertexFormatBits;

            switch (displayStyle)
            {
            case DisplayStyle.HLR:
            case DisplayStyle.FlatColors:
                vertexFormatBits = VertexFormatBits.PositionColored;
                break;

            default:
                vertexFormatBits = VertexFormatBits.PositionNormalColored;
                break;
            }
            var vertexFormat = new VertexFormat(vertexFormatBits);

            var vBuffer = new VertexBuffer(GetVertexSize(vertexFormatBits) * numVertices);
            var iBuffer = new IndexBuffer(numIndices);

            vBuffer.Map(GetVertexSize(vertexFormatBits) * numVertices);
            iBuffer.Map(numIndices);

            var verticesFlat = new List <VertexPositionColored>();
            var vertices     = new List <VertexPositionNormalColored>();
            var triangles    = new List <IndexTriangle>();

            // We duplicate the vertices on each triangle so that
            // we can get the correct number of face normals.
            foreach (var t in mesh.Triangles)
            {
                foreach (var v in t.Vertices)
                {
                    var pos = v.Position.ToXYZFeet();

                    outline.AddPoint(pos);

                    switch (vertexFormatBits)
                    {
                    case VertexFormatBits.PositionColored:
                        var color = displayStyle == DisplayStyle.HLR ? new ColorWithTransparency(255, 255, 255, 0) : v.Color.ToColorWithTransparency();
                        verticesFlat.Add(new VertexPositionColored(pos, color));
                        break;

                    default:
                        vertices.Add(new VertexPositionNormalColored(pos, t.Normal.ToXYZ(), v.Color.ToColorWithTransparency()));
                        break;
                    }

                    if (pos.X < min.X && pos.Y < min.Y && pos.Z < min.Z)
                    {
                        min = pos;
                    }
                    if (pos.X > min.X && pos.Y > min.Y && pos.Z > min.Z)
                    {
                        max = pos;
                    }
                }

                triangles.Add(new IndexTriangle(t.Vertices[0].Index, t.Vertices[1].Index, t.Vertices[2].Index));
            }

            switch (displayStyle)
            {
            case DisplayStyle.HLR:
            case DisplayStyle.FlatColors:
                var pc = vBuffer.GetVertexStreamPositionColored();
                pc.AddVertices(verticesFlat);
                break;

            default:
                var pnc = vBuffer.GetVertexStreamPositionNormalColored();
                pnc.AddVertices(vertices);
                break;
            }

            var iPos = iBuffer.GetIndexStreamTriangle();

            iPos.AddTriangles(triangles);

            vBuffer.Unmap();
            iBuffer.Unmap();

            var effect = new EffectInstance(vertexFormatBits);
            // There is no reason why this should work.
            // In other situations, 255 is the 'full' component.
            // In the case of hidden line rendering, 0, 0, 0 makes white.
            // if (displayStyle == DisplayStyle.HLR)
            // {
            //     var color = new ColorWithTransparency(0, 0, 0, 0);
            //     effect.SetColor(color.GetColor());
            //     effect.SetAmbientColor(color.GetColor());
            //     effect.SetDiffuseColor(color.GetColor());
            // }

            // Create a render data for reuse
            // on non-update calls.
            var renderData = new RenderData()
            {
                VertexBuffer   = vBuffer,
                VertexCount    = numVertices,
                IndexBuffer    = iBuffer,
                IndexCount     = numIndices,
                VertexFormat   = vertexFormat,
                Effect         = effect,
                PrimitiveType  = pType,
                PrimitiveCount = numPrimitives
            };

            if (displayStyle != DisplayStyle.Wireframe && numPrimitives > 0)
            {
                DrawContext.FlushBuffer(vBuffer, numVertices, iBuffer, numIndices, vertexFormat, effect, pType, 0, numPrimitives);
            }

            return(renderData);
        }
        private static RenderData ProcessPrimitive(ILogger logger, glTFLoader.Schema.MeshPrimitive primitive, Gltf gltf, byte[][] buffers, DisplayStyle displayStyle, Outline outline, Transform transform)
        {
            if (primitive.Mode != MeshPrimitive.ModeEnum.TRIANGLES)
            {
                logger.Debug("The selected primitive mode is not supported.");
                return(null);
            }

            var indexAccessor    = gltf.Accessors[(int)primitive.Indices];
            var positionAccessor = gltf.Accessors[primitive.Attributes["POSITION"]];
            var normalAccessor   = gltf.Accessors[primitive.Attributes["NORMAL"]];
            var hasColor         = primitive.Attributes.ContainsKey("COLOR_0");

            var indexBufferView    = gltf.BufferViews[(int)indexAccessor.BufferView];
            var positionBufferView = gltf.BufferViews[(int)positionAccessor.BufferView];
            var normalBufferView   = gltf.BufferViews[(int)normalAccessor.BufferView];

            var indices = new List <int>();

            for (var i = indexBufferView.ByteOffset; i < indexBufferView.ByteOffset + indexBufferView.ByteLength; i += indexBufferView.ByteStride ?? sizeof(ushort))
            {
                var index = BitConverter.ToUInt16(buffers[indexBufferView.Buffer], i);
                indices.Add(index);
            }

            var floatSize = sizeof(float);
            var positions = new List <XYZ>();

            for (var i = positionBufferView.ByteOffset; i < positionBufferView.ByteOffset + positionBufferView.ByteLength; i += positionBufferView.ByteStride ?? (floatSize * 3))
            {
                // Read x, y, z values
                var x  = BitConverter.ToSingle(buffers[positionBufferView.Buffer], i);
                var y  = BitConverter.ToSingle(buffers[positionBufferView.Buffer], i + floatSize);
                var z  = BitConverter.ToSingle(buffers[positionBufferView.Buffer], i + floatSize * 2);
                var pt = new XYZ(Elements.Units.MetersToFeet(x), Elements.Units.MetersToFeet(y), Elements.Units.MetersToFeet(z));
                if (transform != null)
                {
                    pt = transform.OfPoint(pt);
                }
                outline.AddPoint(pt);
                positions.Add(pt);
            }

            var normals = new List <XYZ>();

            for (var i = normalBufferView.ByteOffset; i < normalBufferView.ByteOffset + normalBufferView.ByteLength; i += normalBufferView.ByteStride ?? (floatSize * 3))
            {
                // Read x, y, z values
                var x = BitConverter.ToSingle(buffers[normalBufferView.Buffer], i);
                var y = BitConverter.ToSingle(buffers[normalBufferView.Buffer], i + floatSize);
                var z = BitConverter.ToSingle(buffers[normalBufferView.Buffer], i + floatSize * 2);
                var n = new XYZ(x, y, z);
                if (transform != null)
                {
                    n = transform.OfVector(n);
                }
                normals.Add(n);
            }

            var colors = new List <ColorWithTransparency>();

            if (hasColor)
            {
                var colorAccessor   = gltf.Accessors[primitive.Attributes["COLOR_0"]];
                var colorBufferView = gltf.BufferViews[(int)colorAccessor.BufferView];
                for (var i = colorBufferView.ByteOffset; i < colorBufferView.ByteOffset + colorBufferView.ByteLength; i += colorBufferView.ByteStride ?? (floatSize * 3))
                {
                    // Read x, y, z values
                    var r = BitConverter.ToSingle(buffers[colorBufferView.Buffer], i);
                    var g = BitConverter.ToSingle(buffers[colorBufferView.Buffer], i + floatSize);
                    var b = BitConverter.ToSingle(buffers[colorBufferView.Buffer], i + floatSize * 2);
                    colors.Add(displayStyle == DisplayStyle.HLR ? new ColorWithTransparency(255, 255, 255, 0) : new ColorWithTransparency((uint)(r * 255), (uint)(g * 255), (uint)(b * 255), 0));
                }
            }

            // The number of vertices will be the same as the length of the indices
            // because we'll duplicate vertices at every position.
            var numVertices   = indices.Count;
            var pType         = PrimitiveType.TriangleList;
            var numPrimitives = indices.Count / 3;
            var numIndices    = GetPrimitiveSize(pType) * numPrimitives;

            VertexFormatBits vertexFormatBits;

            switch (displayStyle)
            {
            case DisplayStyle.HLR:
            case DisplayStyle.FlatColors:
                vertexFormatBits = VertexFormatBits.PositionColored;
                break;

            default:
                vertexFormatBits = VertexFormatBits.PositionNormalColored;
                break;
            }
            var vertexFormat = new VertexFormat(vertexFormatBits);

            var vBuffer = new VertexBuffer(GetVertexSize(vertexFormatBits) * numVertices);
            var iBuffer = new IndexBuffer(numIndices);

            vBuffer.Map(GetVertexSize(vertexFormatBits) * numVertices);
            iBuffer.Map(numIndices);

            var verticesFlat = new List <VertexPositionColored>();
            var vertices     = new List <VertexPositionNormalColored>();
            var triangles    = new List <IndexTriangle>();

            ColorWithTransparency color = null;

            if (displayStyle == DisplayStyle.HLR)
            {
                color = new ColorWithTransparency(255, 255, 255, 0);
            }
            else if (primitive.Material != null)
            {
                var material = gltf.Materials[(int)primitive.Material];
                var r        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[0] * 255);
                var g        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[1] * 255);
                var b        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[2] * 255);
                var a        = (uint)(material.PbrMetallicRoughness.BaseColorFactor[3] * 255);
                color = new ColorWithTransparency(r, g, b, a);
            }

            for (var i = 0; i < indices.Count; i += 3)
            {
                var ia = indices[i];
                var ib = indices[i + 1];
                var ic = indices[i + 2];

                var a = positions[ia];
                var b = positions[ib];
                var c = positions[ic];

                var na = normals[ia];
                var nb = normals[ib];
                var nc = normals[ic];

                switch (vertexFormatBits)
                {
                case VertexFormatBits.PositionColored:
                    if (hasColor)
                    {
                        color = colors[ia];
                    }
                    verticesFlat.Add(new VertexPositionColored(a, hasColor ? colors[ia] : color));
                    verticesFlat.Add(new VertexPositionColored(b, hasColor ? colors[ib] : color));
                    verticesFlat.Add(new VertexPositionColored(c, hasColor ? colors[ic] : color));
                    break;

                default:
                    if (hasColor)
                    {
                        color = colors[ia];
                    }
                    vertices.Add(new VertexPositionNormalColored(a, na, hasColor ? colors[ia] : color));
                    vertices.Add(new VertexPositionNormalColored(b, nb, hasColor ? colors[ib] : color));
                    vertices.Add(new VertexPositionNormalColored(c, nc, hasColor ? colors[ic] : color));
                    break;
                }

                triangles.Add(new IndexTriangle(i, i + 1, i + 2));
            }

            switch (displayStyle)
            {
            case DisplayStyle.HLR:
            case DisplayStyle.FlatColors:
                var pc = vBuffer.GetVertexStreamPositionColored();
                pc.AddVertices(verticesFlat);
                break;

            default:
                var pnc = vBuffer.GetVertexStreamPositionNormalColored();
                pnc.AddVertices(vertices);
                break;
            }

            var iPos = iBuffer.GetIndexStreamTriangle();

            iPos.AddTriangles(triangles);

            vBuffer.Unmap();
            iBuffer.Unmap();

            var effect = new EffectInstance(vertexFormatBits);

            var renderData = new RenderData()
            {
                VertexBuffer   = vBuffer,
                VertexCount    = numVertices,
                IndexBuffer    = iBuffer,
                IndexCount     = numIndices,
                VertexFormat   = vertexFormat,
                Effect         = effect,
                PrimitiveType  = pType,
                PrimitiveCount = numPrimitives
            };

            if (displayStyle != DisplayStyle.Wireframe && numPrimitives > 0)
            {
                DrawContext.FlushBuffer(vBuffer, numVertices, iBuffer, numIndices, vertexFormat, effect, pType, 0, numPrimitives);
            }

            return(renderData);
        }
Exemple #5
0
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            _uiapp = commandData.Application;
            _uidoc = _uiapp.ActiveUIDocument;
            _doc   = _uidoc.Document;

            try
            {
                // Get all the floor elements
                FilteredElementCollector floorFec = new FilteredElementCollector(_doc).OfClass(typeof(Floor));
                if (floorFec.Count() != 0)
                {
                    // Create a 3D view in order to ge the outline of the entire model
                    // Get a ViewFamilyType for a 3D view
                    ViewFamilyType viewFamilyType =
                        (from v in new FilteredElementCollector(_doc)
                         .OfClass(typeof(ViewFamilyType))
                         .Cast <ViewFamilyType>()
                         where v.ViewFamily == ViewFamily.ThreeDimensional
                         select v).First();
                    Transaction t = new Transaction(_doc);
                    t.Start("Create 3D View to get the size of the building");
                    View3D view = View3D.CreateIsometric(_doc, viewFamilyType.Id);
                    t.Commit();

                    // Get the outline of the entire 3D model
                    BoundingBoxXYZ ibb     = floorFec.Cast <Floor>().First().get_BoundingBox(view);
                    Outline        outline = new Outline(ibb.Min, ibb.Max);
                    foreach (Floor floor in floorFec)
                    {
                        BoundingBoxXYZ boxXYZ = floor.get_BoundingBox(view);
                        outline.AddPoint(boxXYZ.Min);
                        outline.AddPoint(boxXYZ.Max);
                    }

                    XYZ    dimension  = outline.MaximumPoint - outline.MinimumPoint;
                    double xDimension = UnitUtils.Convert(
                        dimension.X,
                        DisplayUnitType.DUT_DECIMAL_FEET,
                        DisplayUnitType.DUT_METERS);
                    double yDimension = UnitUtils.Convert(
                        dimension.Y,
                        DisplayUnitType.DUT_DECIMAL_FEET,
                        DisplayUnitType.DUT_METERS);

                    double maxLength    = Math.Max(xDimension, yDimension);
                    double minLength    = Math.Min(xDimension, yDimension);
                    bool   scaleIsFound = false;

                    foreach (int scale in _scales)
                    {
                        if (maxLength / scale < Properties.Settings.Default.TITLEBLOCK_LENGTH &&
                            minLength / scale < Properties.Settings.Default.TITLEBLOCK_WIDTH)
                        {
                            SetScaleForm form = new SetScaleForm(scale);
                            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                            {
                                ChangeViewTemplateScaleIfFound(scale);
                            }
                            scaleIsFound = true;
                            break;
                        }
                    }

                    if (!scaleIsFound)
                    {
                        SetScaleForm form = new SetScaleForm(0);
                        form.ShowDialog();
                    }

                    t.Start("Delete the 3D View");
                    _doc.Delete(view.Id);
                    t.Commit();
                }
                else
                {
                    TaskDialog.Show("Revit", "No floor element exists in the document !");
                }
            }
            catch (Exception e)
            {
                message = e.Message;
                return(Result.Failed);
            }

            return(Result.Succeeded);
        }