Ejemplo n.º 1
0
        static private bool CopyObject(ref Va3cContainer.Va3cObject destObject, Va3cContainer.Va3cObject srcObject)
        {
            if (destObject == null || srcObject == null)
            {
                return(false);
            }

            destObject.type     = srcObject.type;
            destObject.name     = srcObject.name;
            destObject.material = srcObject.material;
            destObject.geometry = srcObject.geometry;

            foreach (var child in srcObject.children)
            {
                Va3cContainer.Va3cObject destChild = new Va3cContainer.Va3cObject();
                destChild.uuid = StringConverter.NewGuid();
                if (!CopyObject(ref destChild, child))
                {
                    return(false);
                }
                destObject.children.Add(destChild);
            }

            return(true);
        }
        static private Dictionary <string, JObject> PropertyObjectDic; //  key : property file id, value : property json object

        static public void Initial()
        {
            currentFileId          = StringConverter.NewGuid();
            curentPropertyJsObject = new JObject();
            PropertyObjectDic      = new Dictionary <string, JObject>();
            PropertyObjectDic.Add(currentFileId, curentPropertyJsObject);
        }
        public ErrorType ExportDocumentWithAcvtiveView(Document document)
        {
            string exportFolder = ExportEventHandler.Settings.OutputFolder;

            if (document == null || !Directory.Exists(exportFolder))
            {
                return(ErrorType.Others);
            }

            Container.metadata.generator    = "NDS";
            Container.metadata.type         = "Object";
            Container.metadata.version      = "1.0";
            Container.va3cobject.uuid       = StringConverter.NewGuid();
            Container.va3cobject.type       = "Root Object";
            Container.va3cobject.name       = StringConverter.ToUtf8(Path.GetFileName(document.PathName));
            Container.va3cobject.bHasMatrix = true;
            TransformExporter.GetRootObjectMatrix(ref Container.va3cobject.matrix);

            //Constructs a new FilteredElementCollector that will search and filter the visible elements in a view.
            FilteredElementCollector elems = new FilteredElementCollector(document, document.ActiveView.Id).WhereElementIsNotElementType();

            if (elems.GetElementCount() == 0)
            {
                return(ErrorType.NoElement);
            }

            Dictionary <ElementId, List <Element> > elementsDic = new Dictionary <ElementId, List <Element> >();

            ClassifyElementsByStructureType(elems, ref elementsDic);
            if (ExportEventHandler.Settings.StructureType == CommonSettings.StructureTreeType.ByLevel)
            {
                if (!LevelExporter.ExportLevels(elementsDic, ref Container.va3cobject, document))
                {
                    return(ErrorType.NoElement);
                }
            }
            else if (ExportEventHandler.Settings.StructureType == CommonSettings.StructureTreeType.ByCategory)
            {
                if (!CategoryExporter.ExportCategories(elementsDic, ref Container.va3cobject, document))
                {
                    return(ErrorType.NoElement);
                }
            }

            MaterialExporter.GetAllMaterials(ref Container.materials);
            MaterialExporter.CopyTextureFiles(exportFolder);
            GeometryExporter.GetAllGeometries(ref Container.geometries);
            PropertyExporter.WritePropertyFiles(exportFolder);
            if (Container.va3cobject.children.Count == 0 || Container.geometries.Count == 0)
            {
                return(ErrorType.NoElement);
            }

            if (!Web3DModelWriterManage.Web3DModelWriterManage.WriteWeb3DModelFilesVersion7BufferChunksBvh(ref Container, exportFolder, "model.js", "geom.bin", 2))
            {
                return(ErrorType.UnSupportedGeometry);
            }

            return(ErrorType.Success);;
        }
    static private bool ExportFamily(KeyValuePair <string, List <Element> > familyElements, ref Va3cContainer.Va3cObject categoryObject)
    {
        if (familyElements.Value.Count == 0)
        {
            return(false);
        }

        Va3cContainer.Va3cObject familyObject = new Va3cContainer.Va3cObject();
        familyObject.uuid = StringConverter.NewGuid();
        familyObject.type = "Family";
        familyObject.name = familyElements.Key;

        Dictionary <string, List <Element> > familySymbolElementsDic = new Dictionary <string, List <Element> >();
        List <Element> noFamilySymbolElementsList = new List <Element>();

        foreach (Element element in familyElements.Value)
        {
            string familySymbolName;
            if (GetElementFamilySymbolName(element, out familySymbolName))
            {
                if (familySymbolElementsDic.ContainsKey(familySymbolName))
                {
                    familySymbolElementsDic[familySymbolName].Add(element);
                }
                else
                {
                    familySymbolElementsDic.Add(familySymbolName, new List <Element> {
                            element
                        });
                }
            }
            else
            {
                noFamilySymbolElementsList.Add(element);
            }
        }
        ExportFamilySymbols(familySymbolElementsDic, ref familyObject);

        foreach (Element element in noFamilySymbolElementsList)
        {
            if (!ElementExporter.ExportElement(element, ref familyObject))
            {     /*log*/
            }
        }

        if (familyObject.children.Count > 0)
        {
            categoryObject.children.Add(familyObject);
        }

        return(true);
    }
static private bool ExportFamilySymbol(KeyValuePair <string, List <Element> > familySymbolElements, ref Va3cContainer.Va3cObject familyObject)
{
    Va3cContainer.Va3cObject familySymbolObject = new Va3cContainer.Va3cObject();
    familySymbolObject.uuid = StringConverter.NewGuid();
    familySymbolObject.type = "Family Symbol";
    familySymbolObject.name = familySymbolElements.Key;

    foreach (Element element in familySymbolElements.Value)
    {
        if (!ElementExporter.ExportElement(element, ref familySymbolObject))
        {         /*log*/
        }
    }

    if (familySymbolObject.children.Count > 0)
    {
        familyObject.children.Add(familySymbolObject);
    }

    return(true);
}
Ejemplo n.º 6
0
        static public bool ExportElement(Element element, ref Va3cContainer.Va3cObject categoryObject)
        {
            if (!IsValidElement(element))
            {
                return(false);
            }

            Va3cContainer.Va3cObject elementObject = new Va3cContainer.Va3cObject();
            elementObject.uuid = StringConverter.NewGuid();
            elementObject.type = element.GetType().Name;
            elementObject.name = element.Name;

            //export transform matrix
            if (element is FamilyInstance)
            {
                Transform transform = (element as Instance).GetTransform();
                if (!transform.IsIdentity)
                {
                    elementObject.bHasMatrix = true;
                    if (!TransformExporter.ExportTransform(transform, ref elementObject.matrix))
                    {
                        return(false);
                    }
                }
            }

            //FamilyInstance不用重复导出几何数据
            if (element is FamilyInstance)
            {
                int hashcode = GetFamilyInstanceHashCode(element as FamilyInstance);
                if (hashcode != -1 && _existFamilyInstanceObjects.ContainsKey(hashcode))
                {
                    if (CopyObject(ref elementObject, _existFamilyInstanceObjects[hashcode]))
                    {
                        if (elementObject.children.Count > 0)
                        {
                            ExportParameters(element, ref elementObject);
                            categoryObject.children.Add(elementObject);
                        }

                        return(true);
                    }
                }
            }

            List <GeometryObject> geometryObjects = new List <GeometryObject>();

            if (!GetGeometryObjects(element, ref geometryObjects))
            {
                return(false);
            }
            if (!GeometryExporter.ExportGeometryObject(geometryObjects, ref elementObject, element.Document))
            {
                return(false);
            }

            if (elementObject.children.Count > 0)
            {
                ExportParameters(element, ref elementObject);
                categoryObject.children.Add(elementObject);
            }

            if (element is FamilyInstance)
            {
                int hashcode = GetFamilyInstanceHashCode(element as FamilyInstance);
                if (hashcode != -1 && !_existFamilyInstanceObjects.ContainsKey(hashcode))
                {
                    _existFamilyInstanceObjects.Add(hashcode, elementObject);
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
        static public bool ExportGeometryObject(List <GeometryObject> rvtGeometryObjects, ref Va3cContainer.Va3cObject elementObject, Document document)
        {
            Dictionary <ElementId, List <Face> > faceGroups = new Dictionary <ElementId, List <Face> >();
            Dictionary <ElementId, List <Mesh> > meshGroups = new Dictionary <ElementId, List <Mesh> >();  //按材质分组
            List <Curve>    curveGroup    = new List <Curve>();
            List <PolyLine> polylineGroup = new List <PolyLine>();

            foreach (GeometryObject rvtGeometryObject in rvtGeometryObjects)
            {
                ClassifyGeometryObjects(rvtGeometryObject, document, ref faceGroups, ref meshGroups, ref curveGroup, ref polylineGroup);
            }

            //导出face
            foreach (KeyValuePair <ElementId, List <Face> > faceGroup in faceGroups)
            {
                Va3cContainer.Va3cObject faceGroupObject = new Va3cContainer.Va3cObject();
                faceGroupObject.uuid = StringConverter.NewGuid();
                faceGroupObject.type = "Mesh";
                faceGroupObject.name = "Face Group";
                Material material = document.GetElement(faceGroup.Key) as Material;
                Va3cContainer.Va3cGeometry faceGroupGeometry = new Va3cContainer.Va3cGeometry();
                faceGroupGeometry.uuid = StringConverter.NewGuid();
                faceGroupGeometry.data.nGeometryCategory = eGeometryCategory.Triangle;
                faceGroupObject.geometry = faceGroupGeometry.uuid;
                bool bHasUvs;
                if (ExportFaces(faceGroup.Value, ref faceGroupGeometry, out bHasUvs))
                {
                    if (bHasUvs)
                    {
                        MaterialExporter.ExportMaterial(material, typeof(Face), ref faceGroupObject.material);
                    }
                    else
                    {
                        MaterialExporter.ExportMaterial(material, typeof(Mesh), ref faceGroupObject.material);
                    }
                    elementObject.children.Add(faceGroupObject);
                    Geometries.Add(faceGroupGeometry);
                }
            }


            //导出mesh
            foreach (KeyValuePair <ElementId, List <Mesh> > meshGroup in meshGroups)
            {
                Va3cContainer.Va3cObject meshGroupObject = new Va3cContainer.Va3cObject();
                meshGroupObject.uuid = StringConverter.NewGuid();
                meshGroupObject.type = "Mesh";
                meshGroupObject.name = "Mesh Group";
                Material material = document.GetElement(meshGroup.Key) as Material;
                MaterialExporter.ExportMaterial(material, typeof(Mesh), ref meshGroupObject.material);
                Va3cContainer.Va3cGeometry meshGroupGeometry = new Va3cContainer.Va3cGeometry();
                meshGroupGeometry.uuid = StringConverter.NewGuid();
                meshGroupGeometry.data.nGeometryCategory = eGeometryCategory.Triangle;
                meshGroupObject.geometry = meshGroupGeometry.uuid;

                if (ExportMeshes(meshGroup.Value, ref meshGroupGeometry))
                {
                    elementObject.children.Add(meshGroupObject);
                    Geometries.Add(meshGroupGeometry);
                }
            }

            //导出edge
            Va3cContainer.Va3cObject edgeGroupObject = new Va3cContainer.Va3cObject();
            edgeGroupObject.uuid = StringConverter.NewGuid();
            edgeGroupObject.type = "LinePieces";
            edgeGroupObject.name = "Edge Group";
            MaterialExporter.ExportMaterial(null, typeof(Edge), ref edgeGroupObject.material);
            Va3cContainer.Va3cGeometry edgeGroupGeometry = new Va3cContainer.Va3cGeometry();
            edgeGroupGeometry.uuid = StringConverter.NewGuid();
            edgeGroupGeometry.data.nGeometryCategory = eGeometryCategory.Line;
            edgeGroupObject.geometry = edgeGroupGeometry.uuid;
            if (ExportCurveAndPoliLines(curveGroup, polylineGroup, ref edgeGroupGeometry))
            {
                elementObject.children.Add(edgeGroupObject);
                Geometries.Add(edgeGroupGeometry);
            }

            return(true);
        }
        static public bool ExportParameters(string objectName, string objectId, ParameterMap parameterMap, out string propertyFileName)
        {
            propertyFileName = string.Empty;
            if (parameterMap == null || parameterMap.Size == 0)
            {
                return(false);
            }

            if (curentPropertyJsObject.Count == MaxPropertiesPerSingleFile)
            {
                currentFileId          = StringConverter.NewGuid();
                curentPropertyJsObject = new JObject();
                PropertyObjectDic.Add(currentFileId, curentPropertyJsObject);
            }

            Dictionary <string, List <string> > propertyCategoryDic = new Dictionary <string, List <string> >();
            var itor = parameterMap.ForwardIterator();

            while (itor.MoveNext())
            {
                Parameter  param = itor.Current as Parameter;
                Definition def   = param.Definition;
                if (def == null)
                {
                    continue;
                }
                string categoryName = LabelUtils.GetLabelFor(def.ParameterGroup);
                string paramName    = def.Name;
                if (string.IsNullOrEmpty(paramName))
                {
                    continue;
                }
                string paramValue = "";
                if (!string.IsNullOrEmpty(param.AsString()))
                {
                    paramValue = param.AsString();
                }
                else if (!string.IsNullOrEmpty(param.AsValueString()))
                {
                    paramValue = param.AsValueString();
                }

                if (propertyCategoryDic.ContainsKey(categoryName))
                {
                    propertyCategoryDic[categoryName].Add(paramName);
                    propertyCategoryDic[categoryName].Add(paramValue);
                }
                else
                {
                    propertyCategoryDic.Add(categoryName, new List <string> {
                        paramName, paramValue
                    });
                }
            }

            JObject objectPropertyJObj = new JObject();

            objectPropertyJObj.Add("name", objectName);
            JArray propertyCategoriesJArr = new JArray();

            foreach (var propertyCategory in propertyCategoryDic)
            {
                JObject propertyCategoryJObj = new JObject();
                JArray  propertyJArr         = new JArray(propertyCategory.Value);
                propertyCategoryJObj.Add(propertyCategory.Key, propertyJArr);
                propertyCategoriesJArr.Add(propertyCategoryJObj);
            }
            objectPropertyJObj.Add("PropertyCategories", propertyCategoriesJArr);
            curentPropertyJsObject.Add(objectId, objectPropertyJObj);

            propertyFileName = currentFileId + ".js.gz";
            return(true);
        }
Ejemplo n.º 9
0
    static private bool ExportLevel(KeyValuePair <ElementId, List <Element> > levelElements, ref Va3cContainer.Va3cObject rootObject, Document activeDocument)
    {
        Va3cContainer.Va3cObject levelObject = new Va3cContainer.Va3cObject();
        levelObject.uuid = StringConverter.NewGuid();
        levelObject.type = "Level";
        Level level = activeDocument.GetElement(levelElements.Key) as Level;

        if (level != null)
        {
            levelObject.name = level.Name;
        }
        else
        {
            levelObject.name = "无标高";
        }

        Dictionary <ElementId, List <Element> > CategoryElementsDic = new Dictionary <ElementId, List <Element> >();

        foreach (Element element in levelElements.Value)
        {
            Category  category   = element.Category;
            ElementId categoryId = new ElementId(-1);
            if (category != null)
            {
                categoryId = category.Id;
            }
            else
            {
                categoryId = new ElementId(-1);
            }

            if (CategoryElementsDic.ContainsKey(categoryId))
            {
                CategoryElementsDic[categoryId].Add(element);
            }
            else
            {
                CategoryElementsDic.Add(categoryId, new List <Element> {
                        element
                    });
            }
        }

        if (!CategoryExporter.ExportCategories(CategoryElementsDic, ref levelObject, activeDocument))
        {
            return(false);
        }

        if (level != null)
        {
            string propertyFileName;
            if (PropertyExporter.ExportParameters(levelObject.name, levelObject.uuid, level.ParametersMap, out propertyFileName))
            {
                levelObject.propertyfile = propertyFileName;
            }
        }

        if (levelObject.children.Count > 0)
        {
            rootObject.children.Add(levelObject);
        }

        return(true);
    }
    static private bool ExportCategory(KeyValuePair <ElementId, List <Element> > categoryElements, ref Va3cContainer.Va3cObject rootObject, Document activeDocument)
    {
        if (categoryElements.Value.Count == 0)
        {
            return(false);
        }

        Va3cContainer.Va3cObject categoryObject = new Va3cContainer.Va3cObject();
        categoryObject.uuid = StringConverter.NewGuid();
        categoryObject.type = "Category";

        Category category = Category.GetCategory(activeDocument, categoryElements.Key);

        if (category == null)
        {
            categoryObject.name = "无类别";
        }
        else
        {
            categoryObject.name = category.Name;
        }

        Dictionary <string, List <Element> > familyElementsDic = new Dictionary <string, List <Element> >();
        List <Element> noFamilyElementsList = new List <Element>();

        foreach (Element element in categoryElements.Value)
        {
            string familyName;
            if (GetElementFamilyName(element, out familyName))
            {
                if (familyElementsDic.ContainsKey(familyName))
                {
                    familyElementsDic[familyName].Add(element);
                }
                else
                {
                    familyElementsDic.Add(familyName, new List <Element> {
                            element
                        });
                }
            }
            else
            {
                noFamilyElementsList.Add(element);
            }
        }
        FamilyExporter.ExportFamilys(familyElementsDic, ref categoryObject);

        foreach (Element element in noFamilyElementsList)
        {
            if (!ElementExporter.ExportElement(element, ref categoryObject))
            {     /*log*/
            }
        }

        if (categoryObject.children.Count > 0)
        {
            rootObject.children.Add(categoryObject);
        }

        return(true);
    }