示例#1
0
        private void UploadOBJ(LMAStudio.StreamVR.Common.Models.Family dto, byte[] file_bytes)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                MultipartFormDataContent form = new MultipartFormDataContent();

                form.Add(new StringContent(dto.Name ?? ""), "symbolName");
                form.Add(new StringContent(dto.FamilyName ?? ""), "familyName");
                form.Add(new StringContent(dto.ModelName ?? ""), "modelName");
                form.Add(new StringContent(dto.Manufacturer ?? ""), "publisher");
                form.Add(new StringContent(dto.Description ?? ""), "description");
                form.Add(new StringContent(dto.Tag ?? ""), "tag");
                form.Add(new ByteArrayContent(file_bytes, 0, file_bytes.Length), "file", $"{dto.FamilyId}.obj");

                string url = $"{_modelServerUrl}/api/model/{dto.FamilyId}";
                _log("Uploading model to: " + url);
                HttpResponseMessage response = httpClient.PostAsync(url, form).Result;

                response.EnsureSuccessStatusCode();

                string sd = response.Content.ReadAsStringAsync().Result;
            }
        }
示例#2
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()}"
                });
            }
        }