Friday, February 19, 2016

Dynamo to C# Revit Macros #1 : QR Code Generator

I am gradually learning to code learning C#, and by that I mean windows console applications. Where everyone else starts... As in the hello world and what door would you like to open and win a car sorts of applications. The Bob Tabor videos are helping a lot and I even picked up a Microsoft Step by Step book which is helping out a lot. I intend to post some of my own successes later, but this blog post is directly focused on a collaborative effort towards "Where ideas come from" and how to make these said ideas better.

   
Video share credit to Fausto Mendez and, of course, Steven Johnson 


The story begins back in 2014 where I met my friend and colleague Fausto Mendez, at Autodesk University. As I stood in line anxiously, the morning of my first A.U, I met Fausto at the front board. He was standing there prior to me which surprised me because I was so damn early, due to excitement, that I thought I would be the first one there. Through networking and communicating with Fausto. I found out that he was an experienced coder, and he was all about hacking things. I was impressed with everything he was saying, but was very naive to the full scope of what he was actually coding or what he actually did with all this coding knowledge. I found out things of similarity about his job compared to mine and how he was essentially applying everything I wanted to apply to plumbing and heating, but towards the electrical profession. As the past two A.U.'s have gone by we've stayed in touch and I've seen how his successes have continued, and seen his applications immensely improve. He has been supportive along the way of my desire to learn, and is patiently awaiting my successes, even though it's taking far longer than it should :). Since then I've become a huge user of Dynamo. I've created several modules, and have collaborated in the community with others to implement new nodes that could overall lead to the success of some ideas I've had in our office that could streamline our workflows. This led to me thinking about a new innovation of things, but an innovation of ideas with Fausto. I proposed the idea of collaboratively converting the dynamo modules into C# Code. Through his amazing coding skills and the use of dynamo as a visual flow of the code. Fausto was able to compile the code quicker and it was easier for me to effectively communicate my ideas

Below is the QR Code Generator dynamo module to C# conversion:

I simply made an Autodesk Screencast. Shared the video with him along with the Dynamo module and he was able to quickly respond back with the success of the Revit Macro and shared the modules code in a text file.

Dynamo Module #1: QR Code Generator


Dynamo Module #1 QR Code Generator

This module allows for the creation of QR codes by simply clicking the family and/or Assembly Instance to a folder location, thanks to Jostein Olsen's QR Module.

Fausto's Response:

Autodesk Screencast of Revit Macro in action

Fausto's walkthrough of how to compile and write the C# code illustrated through his website.

http://www.faustomendez.com/2016/02/revit-qrcode-generator/

QrCodeGenerator();

 public void QrCodeGenerator()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;
            
            List<ElementId> ids = new List<ElementId>();
            Selection sel = uidoc.Selection;

            ICollection<ElementId> selIds = sel.GetElementIds();

            if (0 < selIds.Count)
            {
                foreach (ElementId id in selIds)
                {
                    ids.Add(id);
                }
            }

            if (0 == selIds.Count)
            {
                IList<Reference> refs = null;

                try
                {
                    refs = sel.PickObjects(ObjectType.Element,
                    "Please Element(s) to Generate QrCode.");
                }
                catch (Autodesk.Revit.Exceptions
                .OperationCanceledException)
                {
                    //  return Result.Cancelled;
                }
                ids = new List<ElementId>(
                refs.Select<Reference, ElementId>(
                r => r.ElementId));
            }

            foreach (ElementId id in ids)
            {
                Element el = doc.GetElement(id);
                FamilyInstance elsym = el as FamilyInstance;
                FamilySymbol famsym = elsym.Symbol as FamilySymbol;
                string data = Uri.EscapeUriString(famsym.get_Parameter(BuiltInParameter.ALL_MODEL_URL).AsString());
                string folderpath = @"C:\{Your Folder Path Here}\";
                string Filename = el.Name;

                GenerateQRCode(data, folderpath, Filename);
            }


        }

        private static void GenerateQRCode(string Data, string folderpath, string fileName)
        {
            try
            {
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://api.qrserver.com/v1/create-qr-code/?size=900x900&data=" + Data);
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                if ((response.StatusCode == System.Net.HttpStatusCode.OK ||
                    response.StatusCode == System.Net.HttpStatusCode.Moved ||
                    response.StatusCode == System.Net.HttpStatusCode.Redirect) &&
                    response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
                {
                    using (System.IO.Stream inputStream = response.GetResponseStream())
                    using (System.IO.Stream outputStream = System.IO.File.OpenWrite(folderpath + fileName + ".png"))
                    {
                        byte[] buffer = new byte[4096];
                        int bytesRead;
                        do
                        {
                            bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                            outputStream.Write(buffer, 0, bytesRead);
                        } while (bytesRead != 0);
                    }
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error", ex.Message);
            }

        }
Photo credit to Fausto Mendez

This truly shows the power of the Revit community, and that if you set your mind to collaborating with other design professionals your "collision of smaller hunches" will lead to a potentially better idea!

10 comments:

  1. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! video intercom system installation

    ReplyDelete
  2. I exploit solely premium quality products -- you will observe these individuals on: Bitcoin QR Code Generator

    ReplyDelete
  3. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for bitcoin halving date in the whole USA? We are providing bitcoin halving date, bitcoin halving, bitcoin pro app, bitcoin lifestyle, bitcoin era scam, bitcoin era, bitcoin storm, bitcoin circuit with the well price and our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  4. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for bitcoin halving countdown in the whole USA? We are providing Free Bitcoin QR Codes, BTC QR Code, BTC QR, BTC Address To QRCode, Bitcoin QR Maker, bitcoin qr generator with amount, bitcoin qr code generator blockchain, Bitcoin QR CodeGenerator and our services are very fast.
    Click here for Contact: +1 626 245 8049 Email: disujarock2312@gmail.com

    ReplyDelete
  5. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Most secure cryptocurrency in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  6. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for Bitcoin Investment in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  7. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for The bitcoin investmen The Digital trading online in the whole USA? We are providing Augment Trade, most secure cryptocurrency, digital trading, Blockchain Technology, buy bitcoins online, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  8. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for forex broker in the whole USA? We are providing Augment Trade, most secure cryptocurrency, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker,digital trading, Blockchain Technology, buy bitcoins online, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  9. Hi there,
    Thank you so much for the post you do and also I like your post, Are you looking for digital trading in the whole USA? We are providing Augment Trade, most secure cryptocurrency, Bitcoin users, The Cryptocurrency, bitcoin/Forex broker,digital trading, Blockchain Technology, buy bitcoins online, bitcoin address , Bitcoin investment, with the well price and our services are very fast.
    Click here for MORE DETAILS......
    Email us at: info@augment-trade1.com

    ReplyDelete
  10. QR Code Chimp allows you to create the most awesome-looking QR codes in shapes and designs, you have never seen before.
    QR Generator, QR Code Generator, QR Code, QR code online generator, QR code Maker, QR code scanner

    ReplyDelete