コード例 #1
0
        public Message Execute(Document doc, Message msg)
        {
            JObject msgData   = JObject.Parse(msg.Data);
            string  elementId = msgData["Id"].ToString();

            object  element = doc.GetElement(new ElementId(Int32.Parse(elementId)));
            JObject dto     = _converter.ConvertToDTO(element);

            return(new Message
            {
                Type = "CURRENT_STATE",
                Data = JsonConvert.SerializeObject(dto)
            });
        }
コード例 #2
0
        public Message Execute(Document doc, Message msg)
        {
            _log("EXECUTE CREATE");

            JObject dto = JObject.Parse(msg.Data);

            _log("GOT DTO");
            _log(JsonConvert.SerializeObject(dto));

            _log("GETTING ELEMENT");

            JObject response;

            Autodesk.Revit.DB.FamilyInstance newFamily;

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Create Element");

                response = _converter.CreateFromDTO <Autodesk.Revit.DB.FamilyInstance>(doc, dto, out newFamily);
                _log($"Created element {newFamily?.ToString() ?? "NULL"}");
                _log($" - Id {newFamily.Id?.ToString() ?? "NULL"}");
                _log($" - Fam {newFamily.Symbol?.Family?.Id?.ToString() ?? "NULL"}");
                _log($" - Origin {newFamily.GetTransform()?.Origin?.ToString() ?? "NULL"}");

                tx.Commit();
            }

            if (response["ERROR"] == null && newFamily == null)
            {
                response          = new JObject();
                response["ERROR"] = 1;
                response["Msg"]   = "New Family Is Null...";
            }
            if (response["ERROR"] == null)
            {
                response = _converter.ConvertToDTO(newFamily);
            }

            _log("NEW VALUE");
            _log(JsonConvert.SerializeObject(response));

            return(new Message
            {
                Type = "VALUE",
                Data = JsonConvert.SerializeObject(response)
            });
        }
コード例 #3
0
        public Message Execute(Document doc, Message msg)
        {
            _log("EXECUTE SET");

            JObject dto = JObject.Parse(msg.Data);

            _log("GOT DTO");
            _log(JsonConvert.SerializeObject(dto));

            _log("GETTING ELEMENT");

            Element dbValue = doc.GetElement(new ElementId(Int32.Parse(dto["Id"].ToString())));

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Set Element");

                // Ensure warnings are suppressed and failures roll-back
                var failureOptions = tx.GetFailureHandlingOptions();
                failureOptions.SetFailuresPreprocessor(new WarningSupressor());
                tx.SetFailureHandlingOptions(failureOptions);
                _log($"GOT FAMILY INSTANCE {dbValue?.Id.ToString()}");

                // Map dto values to DB
                _converter.MapFromDTO(dto, dbValue);

                tx.Commit();
            }

            _log($"MAPPED FAMILY INSTANCE");

            dto = _converter.ConvertToDTO(dbValue);

            _log("NEW VALUE");
            _log(JsonConvert.SerializeObject(dto));

            _uiDocument.RefreshActiveView();

            return(new Message
            {
                Type = "VALUE",
                Data = JsonConvert.SerializeObject(dto)
            });
        }
コード例 #4
0
        public Message Execute(Document doc, Message msg)
        {
            JObject msgData   = JObject.Parse(msg.Data);
            string  elementId = msgData["Id"].ToString();

            FamilySymbol family = doc.GetElement(new ElementId(Int32.Parse(elementId))) as FamilySymbol;

            GeometryElement geometry = family.get_Geometry(new Options());

            if (geometry == null)
            {
                return(new Message
                {
                    Type = "EMPTY",
                    Data = $"No geometry found for family: {family.Name} ({family.Id})"
                });
            }

            byte[] file_bytes = GeometryToOBJ(doc, geometry);

            try
            {
                System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
                s.Start();

                List <Task> uploadTasks = new List <Task>();

                LMAStudio.StreamVR.Common.Models.Family dto = _converter.ConvertToDTO(family).ToObject <LMAStudio.StreamVR.Common.Models.Family>();

                uploadTasks.Add(Task.Run(() => UploadOBJ(dto, file_bytes)));

                IEnumerable <FamilyInstance> instances = new FilteredElementCollector(doc).
                                                         OfClass(typeof(FamilyInstance)).
                                                         Select(f => f as FamilyInstance).
                                                         Where(f => f.Symbol.Id == family.Id);

                // Get all different variants
                Dictionary <int, FamilyInstance> variants = new Dictionary <int, FamilyInstance>();
                foreach (var f in instances)
                {
                    var info = f.GetOrderedParameters().Where(p => p.Definition.ParameterGroup == BuiltInParameterGroup.PG_GEOMETRY);
                    Dictionary <string, string> infoDict = new Dictionary <string, string>();
                    foreach (var i in info)
                    {
                        infoDict[i.Definition.Name] = i.AsValueString();
                    }
                    int hash = JsonConvert.SerializeObject(infoDict).GetHashCode();
                    variants[hash] = f;
                }

                _log($"{family.Name} has {instances.Count()} varaints");

                // Upload each distinct variant
                foreach (var kv in variants)
                {
                    GeometryElement  variantGeometry = kv.Value.get_Geometry(new Options());
                    GeometryInstance variantInstance = variantGeometry.Where(
                        g => (g as Solid) == null || (g as Solid).Faces.Size == 0
                        ).FirstOrDefault() as GeometryInstance;
                    if (variantInstance == null)
                    {
                        _log($"INSTANCE GEOMETRY NULL FOR: {kv.Value.Name}");
                        continue;
                    }
                    GeometryElement variantInstanceGeometry = variantInstance.GetSymbolGeometry();
                    if (variantInstanceGeometry != null)
                    {
                        byte[] variantFileBytes = GeometryToOBJ(doc, variantInstanceGeometry);
                        uploadTasks.Add(Task.Run(() => UploadOBJVariant(dto.FamilyId, kv.Key.ToString(), variantFileBytes)));
                    }
                }

                Task.WaitAll(uploadTasks.ToArray());

                _log($"Upload time for {instances.Count() + 1} models: {s.ElapsedMilliseconds}ms");
                s.Stop();

                return(new Message
                {
                    Type = "OBJ",
                    Data = dto.FamilyId
                });
            }
            catch (Exception e)
            {
                return(new Message
                {
                    Type = "ERROR",
                    Data = $"Error: {family.Name} ({family.Id}) {e.ToString()}"
                });
            }
        }
コード例 #5
0
        public Message Execute(Document doc, Message msg)
        {
            JObject msgData   = JObject.Parse(msg.Data);
            string  elementId = msgData["Id"].ToString();

            Material mat = doc.GetElement(new ElementId(Int32.Parse(elementId))) as Material;

            try
            {
                System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch();
                s.Start();

                LMAStudio.StreamVR.Common.Models.Material dto = _converter.ConvertToDTO(mat).ToObject <LMAStudio.StreamVR.Common.Models.Material>();

                AppearanceAssetElement assetElement = doc.GetElement(mat.AppearanceAssetId) as AppearanceAssetElement;
                Asset asset = assetElement?.GetRenderingAsset();

                if (asset == null)
                {
                    return(new Message
                    {
                        Type = "EMPTY",
                        Data = $"No material asset found for: {mat.Name} ({mat.Id})"
                    });
                }

                string bmpPathFull = "";

                for (int i = 0; i < asset.Size; i++)
                {
                    AssetProperty prop = asset.Get(i);
                    if (textureProperties.Contains(prop.Name) && prop.NumberOfConnectedProperties > 0)
                    {
                        Asset connectedAsset = prop.GetSingleConnectedAsset();
                        for (int j = 0; j < connectedAsset.Size; j++)
                        {
                            AssetProperty prop2 = connectedAsset.Get(j);
                            if (prop2.Name == "unifiedbitmap_Bitmap")
                            {
                                bmpPathFull = (prop2 as AssetPropertyString).Value;
                            }
                        }
                    }
                }

                if (string.IsNullOrEmpty(bmpPathFull))
                {
                    return(new Message
                    {
                        Type = "EMPTY",
                        Data = $"No exportable material found for: {mat.Name} ({mat.Id})"
                    });
                }

                string texturesDirectory = "C:\\Program Files (x86)\\Common Files\\Autodesk Shared\\Materials\\Textures";
                string bmpPath           = bmpPathFull.Split('|').LastOrDefault();

                string fullPath         = texturesDirectory + "\\" + bmpPath;
                string materialFileName = new FileInfo(fullPath).Name;

                byte[] materialAlbedo = File.ReadAllBytes(fullPath);

                UploadOBJ(dto, materialAlbedo, materialFileName);

                _log($"Export/Upload time for {1} materials: {s.ElapsedMilliseconds}ms");
                s.Stop();

                return(new Message
                {
                    Type = "MAT",
                    Data = mat.Id.ToString()
                });
            }
            catch (Exception e)
            {
                return(new Message
                {
                    Type = "ERROR",
                    Data = $"Error: {mat.Name} ({mat.Id}) {e.ToString()}"
                });
            }
        }