Exemple #1
0
        /// <summary>
        /// generates pricing request JSON - no category filter -  save as file
        /// </summary>
        /// <param name="commandData">An object that is passed to the external application
        /// which contains data related to the command,
        /// such as the application object and active view.</param>
        /// <param name="message">A message that can be set by the external application
        /// which will be displayed if a failure or cancellation is returned by
        /// the external command.</param>
        /// <param name="elements">A set of elements to which the external application
        /// can add elements that are to be highlighted in case of failure or cancellation.</param>
        /// <returns>Return the status of the external command.
        /// A result of Succeeded means that the API external method functioned as expected.
        /// Cancelled can be used to signify that the user cancelled the external operation
        /// at some point. Failure should be returned if the application is unable to proceed with
        /// the operation.</returns>
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
                                                ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            // NOTES: Anything can be done in this method, such as create a message box,
            // a task dialog or fetch some information from revit and so on.
            // We mainly use the task dialog for example.

            // Get the application and document from external command data.
            Application app    = commandData.Application.Application;
            Document    curDoc = commandData.Application.ActiveUIDocument.Document;

            //loads from settings.txt
            settings.LoadSettings();
            revit_pricing_request price1 = utilities.get_pricing_request(curDoc, new List <string>());

            string serialS = JsonConvert.SerializeObject(price1);

            System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog1.Filter = "Text file|*.txt";
            saveFileDialog1.Title  = "Save Query";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                System.IO.File.WriteAllText(saveFileDialog1.FileName, serialS);
            }

            return(Autodesk.Revit.UI.Result.Succeeded);
        }
        public static revit_pricing_request get_pricing_request(Document curDoc, List <string> categories)
        {
            revit_pricing_request price1 = new revit_pricing_request();

            price1.project_id = curDoc.ProjectInformation.UniqueId.ToString();//.ToLower().Replace(" ", "_");

            //formatted snake_case
            List <string> formCat = new List <string>();

            foreach (string cate in categories)
            {
                formCat.Add(cate);//.ToLower().Replace(" ", "_"));
            }


            //filter for family instances
            FilteredElementCollector famCol   = new FilteredElementCollector(curDoc).OfClass(typeof(FamilyInstance));
            IList <Element>          famElems = famCol.ToElements();

            //iterate
            foreach (Element myE in famElems)
            {
                p_element fIelem = new p_element();
                fIelem.id   = myE.Id.ToString(); //.ToLower().Replace(" ", "_");
                fIelem.name = myE.Name;          //.ToLower().Replace(" ", "_");
                fIelem.element_class_name = "Family Instance";
                FamilyInstance myFamInst = myE as FamilyInstance;
                fIelem.properties.Add("family_name", myFamInst.Symbol.FamilyName);  //.ToLower().Replace(" ", "_"));
                fIelem.properties.Add("family_symbol_name", myFamInst.Symbol.Name); //.ToLower().Replace(" ", "_"));

                foreach (Parameter myParam in myFamInst.Parameters)
                {
                    if (myParam.StorageType == StorageType.String)
                    {
                        if (myParam.AsString() != "" && myParam.AsString() != "null" && myParam.AsString() != null)
                        {
                            if (!fIelem.properties.ContainsKey(myParam.Definition.Name.ToLower().Replace(" ", "_")))
                            {
                                fIelem.properties.Add(myParam.Definition.Name.ToLower().Replace(" ", "_"), myParam.AsString());//.ToLower().Replace(" ", "_"));
                            }
                            else
                            {
                                fIelem.properties[myParam.Definition.Name.ToLower().Replace(" ", "_")] += "," + myParam.AsString();
                            }
                        }
                    }
                    else
                    {
                        if (myParam.AsValueString() != "" && myParam.AsValueString() != "null" && myParam.AsValueString() != null)
                        {
                            if (!fIelem.properties.ContainsKey(myParam.Definition.Name.ToLower().Replace(" ", "_")))
                            {
                                fIelem.properties.Add(myParam.Definition.Name.ToLower().Replace(" ", "_"), myParam.AsValueString());//.ToLower().Replace(" ", "_"));
                            }
                            else
                            {
                                fIelem.properties[myParam.Definition.Name.ToLower().Replace(" ", "_")] += "," + myParam.AsValueString();
                            }
                        }
                    }
                }
                if (categories.Count != 0)
                {
                    if (fIelem.properties.ContainsKey("category"))
                    {
                        foreach (string myc in formCat)
                        {
                            if (fIelem.properties["category"].Contains(myc))
                            {
                                price1.elements.Add(fIelem);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    price1.elements.Add(fIelem);
                }
            }
            ElementClassFilter       FamilyFilter    = new ElementClassFilter(typeof(Family));
            FilteredElementCollector FamilyCollector = new FilteredElementCollector(curDoc);
            ICollection <Element>    AllFamilies     = FamilyCollector.WherePasses(FamilyFilter).ToElements();

            foreach (Family Fmly in AllFamilies)
            {
                string FamilyName = Fmly.Name;



                foreach (ElementId symid in Fmly.GetFamilySymbolIds())
                {
                    FamilySymbol FmlyS = curDoc.GetElement(symid) as FamilySymbol;

                    p_element fIelem = new p_element();
                    fIelem.id   = FmlyS.Id.ToString(); //.ToLower().Replace(" ", "_");
                    fIelem.name = FmlyS.Name;          //.ToLower().Replace(" ", "_");
                    fIelem.element_class_name = "Family Symbol";

                    fIelem.properties.Add("family_name", FamilyName);        //.ToLower().Replace(" ", "_"));
                    fIelem.properties.Add("family_symbol_name", FmlyS.Name); //.ToLower().Replace(" ", "_"));



                    foreach (Parameter myParam in FmlyS.Parameters)
                    {
                        if (myParam.StorageType == StorageType.String)
                        {
                            if (myParam.AsString() != "" && myParam.AsString() != "null" && myParam.AsString() != null)
                            {
                                if (!fIelem.properties.ContainsKey(myParam.Definition.Name.ToLower().Replace(" ", "_")))
                                {
                                    fIelem.properties.Add(myParam.Definition.Name.ToLower().Replace(" ", "_"), myParam.AsString());//.ToLower().Replace(" ", "_"));
                                }
                                else
                                {
                                    fIelem.properties[myParam.Definition.Name.ToLower().Replace(" ", "_")] += "," + myParam.AsString();
                                }
                            }
                        }
                        else
                        {
                            if (myParam.AsValueString() != "" && myParam.AsValueString() != "null" && myParam.AsValueString() != null)
                            {
                                if (!fIelem.properties.ContainsKey(myParam.Definition.Name.ToLower().Replace(" ", "_")))
                                {
                                    fIelem.properties.Add(myParam.Definition.Name.ToLower().Replace(" ", "_"), myParam.AsValueString());//.ToLower().Replace(" ", "_"));
                                }
                                else
                                {
                                    fIelem.properties[myParam.Definition.Name.ToLower().Replace(" ", "_")] += "," + myParam.AsValueString();
                                }
                            }
                        }
                    }
                    if (categories.Count != 0)
                    {
                        if (fIelem.properties.ContainsKey("category"))
                        {
                            foreach (string myc in formCat)
                            {
                                if (fIelem.properties["category"].Contains(myc))
                                {
                                    price1.elements.Add(fIelem);
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        price1.elements.Add(fIelem);
                    }
                    //    if (myParam.StorageType == StorageType.String)
                    //    {
                    //        if (!fIelem.properties.ContainsKey(myParam.Definition.Name.ToLower().Replace(" ", "_")) && myParam.AsString() != "" && myParam.AsString() != "null" && myParam.AsString() != null)
                    //            fIelem.properties.Add(myParam.Definition.Name.ToLower().Replace(" ", "_"), myParam.AsString());//.ToLower().Replace(" ", "_"));

                    //    }
                    //    else
                    //    {
                    //        if (!fIelem.properties.ContainsKey(myParam.Definition.Name.ToLower().Replace(" ", "_")) && myParam.AsValueString() != "" && myParam.AsValueString() != "null" && myParam.AsValueString() != null)
                    //            fIelem.properties.Add(myParam.Definition.Name.ToLower().Replace(" ", "_"), myParam.AsValueString());//.ToLower().Replace(" ", "_"));
                    //    }
                    //}

                    //if (categories.Count != 0)
                    //{
                    //    if (fIelem.properties.ContainsKey("category"))
                    //    {
                    //        if (formCat.Contains(fIelem.properties["category"]))
                    //        {
                    //            price1.elements.Add(fIelem);
                    //        }
                    //    }

                    //}
                    //else
                    //{
                    //    price1.elements.Add(fIelem);
                    //}
                }
            }
            return(price1);
        }
Exemple #3
0
        /// <summary>
        /// retrieves categories from server, generates price requests, sends, receives prices and updates model with parameter name in file parameter.txt in same folder as dll
        /// </summary>
        /// <param name="commandData">An object that is passed to the external application
        /// which contains data related to the command,
        /// such as the application object and active view.</param>
        /// <param name="message">A message that can be set by the external application
        /// which will be displayed if a failure or cancellation is returned by
        /// the external command.</param>
        /// <param name="elements">A set of elements to which the external application
        /// can add elements that are to be highlighted in case of failure or cancellation.</param>
        /// <returns>Return the status of the external command.
        /// A result of Succeeded means that the API external method functioned as expected.
        /// Cancelled can be used to signify that the user cancelled the external operation
        /// at some point. Failure should be returned if the application is unable to proceed with
        /// the operation.</returns>
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
                                                ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            // NOTES: Anything can be done in this method, such as create a message box,
            // a task dialog or fetch some information from revit and so on.
            // We mainly use the task dialog for example.

            // Get the application and document from external command data.
            Application app    = commandData.Application.Application;
            Document    curDoc = commandData.Application.ActiveUIDocument.Document;


            //loads from settings.txt
            settings.LoadSettings();


            string     codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri      = new UriBuilder(codeBase);
            string     path     = Uri.UnescapeDataString(uri.Path);

            string paramtextfile = Path.Combine(Path.GetDirectoryName(path), "parametername.txt");

            if (!File.Exists(paramtextfile))
            {
                System.Windows.Forms.MessageBox.Show(paramtextfile + " not found!");
                return(Autodesk.Revit.UI.Result.Failed);
            }

            string paramname = File.ReadAllText(paramtextfile);

            List <string> cats = new List <string>();

            PricingHttpRest.InitializeClient(settings.APILocation, "");
            string configJSON = "";

            try
            {
                PricingHttpRest.APICommand = "pricing/config";
                string result = PricingHttpRest.Get();

                configJSON = PricingHttpRest.receivedContent;
                if (result != "OK")
                {
                    System.Windows.Forms.MessageBox.Show("Error accessing API while getting config");
                    return(Autodesk.Revit.UI.Result.Failed);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error accessing API while getting config");
                return(Autodesk.Revit.UI.Result.Failed);
            }


            try
            {
                Newtonsoft.Json.Linq.JToken jt   = Newtonsoft.Json.Linq.JObject.Parse(configJSON);
                Newtonsoft.Json.Linq.JToken jfil = jt.SelectToken("filters");
                Newtonsoft.Json.Linq.JToken jcat = jfil.SelectToken("categories");
                cats = jcat.ToObject <List <string> >();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Deserialization failure: " + ex.ToString());
                return(Autodesk.Revit.UI.Result.Failed);
            }
            if (cats.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("No categories found, taking all");
            }
            //if (myConf.filters.Count==0)
            //{
            //    System.Windows.Forms.MessageBox.Show("No config data");
            //    return Autodesk.Revit.UI.Result.Failed;
            //}

            revit_pricing_request price1 = utilities.get_pricing_request(curDoc, cats);


            //send element data
            string serialS    = JsonConvert.SerializeObject(price1);
            string pricesJSON = "";

            try
            {
                PricingHttpRest.APICommand  = "pricing/model1";
                PricingHttpRest.sendContent = serialS;

                string result = PricingHttpRest.Post();

                pricesJSON = PricingHttpRest.receivedContent;
                if (result != "OK")
                {
                    System.Windows.Forms.MessageBox.Show("Error accessing API while submitting data for pricing: " + result + " " + pricesJSON);
                    return(Autodesk.Revit.UI.Result.Failed);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error accessing API while submitting  data for pricing: " + ex.ToString());
                return(Autodesk.Revit.UI.Result.Failed);
            }
            //extract id / cost from response
            List <PriceResponseElement> plist = new List <PriceResponseElement>();

            try
            {
                plist = Newtonsoft.Json.JsonConvert.DeserializeObject <List <PriceResponseElement> >(pricesJSON);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Deserialization failure: " + ex.ToString());
                return(Autodesk.Revit.UI.Result.Failed);
            }


            if (plist.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("No instance / cost found");
                return(Autodesk.Revit.UI.Result.Failed);
            }
            List <string> failures = new List <string>();

            foreach (PriceResponseElement ple in plist)
            {
                try
                {
                    bool res = PricingRevitInteraction.UpdateElementParameterValueDouble(curDoc, ple.id, paramname, ple.cost);
                    if (!res)
                    {
                        failures.Add(ple.id);
                    }
                }
                catch (Exception ex)
                {
                    failures.Add(ple.id);
                }
            }

            if (failures.Count > 0)
            {
                System.Windows.Forms.MessageBox.Show("Failed to update some elements: " + string.Join(",", failures.ToArray()));
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Successfully updated " + plist.Count + " elements");
            }


            using (Transaction t = new Transaction(curDoc, "Regen"))
            {
                t.Start();


                curDoc.Regenerate();
                t.Commit();
            }

            return(Autodesk.Revit.UI.Result.Succeeded);
        }
Exemple #4
0
        /// <summary>
        /// retrieves categories from server, generates price requests, sends, receives prices and saves to text file
        /// </summary>
        /// <param name="commandData">An object that is passed to the external application
        /// which contains data related to the command,
        /// such as the application object and active view.</param>
        /// <param name="message">A message that can be set by the external application
        /// which will be displayed if a failure or cancellation is returned by
        /// the external command.</param>
        /// <param name="elements">A set of elements to which the external application
        /// can add elements that are to be highlighted in case of failure or cancellation.</param>
        /// <returns>Return the status of the external command.
        /// A result of Succeeded means that the API external method functioned as expected.
        /// Cancelled can be used to signify that the user cancelled the external operation
        /// at some point. Failure should be returned if the application is unable to proceed with
        /// the operation.</returns>
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData,
                                                ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            // NOTES: Anything can be done in this method, such as create a message box,
            // a task dialog or fetch some information from revit and so on.
            // We mainly use the task dialog for example.

            // Get the application and document from external command data.
            Application app    = commandData.Application.Application;
            Document    curDoc = commandData.Application.ActiveUIDocument.Document;

            //loads from settings.txt
            settings.LoadSettings();
            List <string> cats = new List <string>();

            PricingHttpRest.InitializeClient(settings.APILocation, "");
            string configJSON = "";

            try
            {
                PricingHttpRest.APICommand = "pricing/config";
                string result = PricingHttpRest.Get();

                configJSON = PricingHttpRest.receivedContent;
                if (result != "OK")
                {
                    System.Windows.Forms.MessageBox.Show("Error accessing API while getting config");
                    return(Autodesk.Revit.UI.Result.Failed);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error accessing API while getting config");
                return(Autodesk.Revit.UI.Result.Failed);
            }


            try
            {
                Newtonsoft.Json.Linq.JToken jt   = Newtonsoft.Json.Linq.JObject.Parse(configJSON);
                Newtonsoft.Json.Linq.JToken jfil = jt.SelectToken("filters");
                Newtonsoft.Json.Linq.JToken jcat = jfil.SelectToken("categories");
                cats = jcat.ToObject <List <string> >();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Deserialization failure: " + ex.ToString());
                return(Autodesk.Revit.UI.Result.Failed);
            }
            if (cats.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("No categories found, taking all");
            }
            //if (myConf.filters.Count==0)
            //{
            //    System.Windows.Forms.MessageBox.Show("No config data");
            //    return Autodesk.Revit.UI.Result.Failed;
            //}

            revit_pricing_request price1 = utilities.get_pricing_request(curDoc, cats);


            //send element data
            string serialS    = JsonConvert.SerializeObject(price1);
            string pricesJSON = "";

            try
            {
                PricingHttpRest.APICommand  = "pricing/model1";
                PricingHttpRest.sendContent = serialS;

                string result = PricingHttpRest.Post();

                pricesJSON = PricingHttpRest.receivedContent;
                if (result != "OK")
                {
                    System.Windows.Forms.MessageBox.Show("Error accessing API while submitting  data for pricing: " + result + " " + pricesJSON);
                    return(Autodesk.Revit.UI.Result.Failed);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error accessing API while submitting  data for pricing: " + ex.ToString());
                return(Autodesk.Revit.UI.Result.Failed);
            }


            System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            saveFileDialog1.Filter = "Text file|*.txt";
            saveFileDialog1.Title  = "Save Price / Instance Data";
            saveFileDialog1.ShowDialog();

            // If the file name is not an empty string open it for saving.
            if (saveFileDialog1.FileName != "")
            {
                System.IO.File.WriteAllText(saveFileDialog1.FileName, pricesJSON);
            }

            return(Autodesk.Revit.UI.Result.Succeeded);
        }