Category: .net (c#)

I am going to post a series of articles which attempts to explain complicated ideas in a simple fashion.

  • The importance of identifying the assumptions Behind Every Problem

    My estimable colleague Rafik Ben made a blog post concerning a problem.

    For reference, here it is: http://routetomastery.com/blog/2017/01/08/has-pair-with-some-problem/

    So what’s so good about Rafi’s problem?

    Well like most things in the world, the problem, is not the actual problem. When it comes to computer science, the *actual* problem lies in identifying or understanding it. And the assumptions one makes are no less important.

    Let’s see this in example:

    > A man and his son are driving in a car one day, when they get into a fatal accident. The man is killed instantly. The boy is knocked unconscious, but he is still alive. He is rushed to hospital, and will need immediate surgery. The doctor enters the emergency room, looks at the boy, and says…

    > “I can’t operate on this boy, he is my son.”

    > *How is this possible?*

    It’s one of those things where you either have the answer instantly or you will never get it. It’s because your underlying assumptions and expectations were hidden and/or incorrect. The thesis of this post is this: identify the assumptions inherent behind every problem. For in doing so, you will be more likely to solve it successfully.

    Assumptions area always inherent in a problem

    Every problem has its own assumptions. And these assumptions will drive very different solutions. For example, in the post Rafi made:

    • what if one assumes that the input into the “Pair with sum” function was a disorered list vs ordered list?
    • What if one assumed that the input contained invalid data?
    • Each assumption would in turn require it’s own unique solution. And those solutions might be drastically different. It should be remembered that not all solutions were created equal.

     Assumptions to consider when formulating algorithms

     Some assumptions which I feel are important when considering problems:

    1. What are the domain/range of the inputs and outputs? (e.g. positive or negative, above zero or less than 400)
    2. What are the characteristics of the inputs/outputs? (i.e. divisible by 2, integers only, irrational numbers?).
    3. The data structure(s) of the inputs/outputs?
    4. Can you think of a structure that will make your algorithm more efficient.
    5. Is near enough, good enough? What are the costs of inaccuracy? Can we later fix inaccuracies?
    6. Time limitations?
    7. Computational power limitations?
    8. Human factors:
    • Remember, algorithms are created for the user. You might have to compromise on efficiency and/or accuracy in order to meet this need.  For example: I am positively sure that Google can do a  lot better in its search results: but the boffins there are acutely aware that it’s bad business if the user is left waiting for a search result for more than a few seconds. Consequently, there are some compromises on accuracy in favor of speed. These factors are essential when considering a solution to a problem.

    Another human related issue: the problem must be understandable to the coder, and easily maintainable:

    1. Can you easily understand the problem by looking at it? Easily understood code makes maintenance a breeeze.
    2. What parts do you think will change?

    Summary

    Understand the problem and its assumptions, and you’re halfway there.

    Here is a gist basically listing the above:

    https://gist.github.com/BKSpurgeon/69f624f959e80a7842a2a319d797f120

  • What does GroupBy do – Simple Explanation (Linq)

    What is GroupBy meant to do – Simple Explanation

    As always we will try to formulate a simple explanation of what is really going on.

    Airport

    Imagine you are at an airport and (as always) there is a queue and planes are delayed. See below. What you will immediately notice is that all of the passengers are grouped together promiscuously – standing around in slumps in no particular order. Every body is mixed up with each other.

    But, when you get into the plane, everybody is grouped into three distinct orders:

    1. First class
    2. Business class
    3. Third class. These classes are defined today by folks’ ability to pay. Unlike the olden days. Anyways, let’s move on.

    That’s basically what group by does. It takes a promiscuous group and separates them into distinct orders – in this case, three distinct groups of people: first, business and third class.

    So now you can say:

    • Get me all the names of all the people in first class.
    • Get me the names of all the people in the middle class who live in Europe.

    And it’s as simple as that! As for the technicalities: please refer to the MSDN documentation – hopefully reading it won’t cause too much discomfort.

  • c# .Net Autocad plugin – “Hello world” Walk through

    There’s not much out there in the way of introductions. You’d have to wade through some manuals and it can be tedious. A simple walk through of how to get started. You would do well to record it at 1.5-x2 playback speed.

     

    Autocad .net c# Plug in – Hello World Example from Tek1 on Vimeo.

  • Convert ObjectID[] to ObjectIDCollections!

    When dealing with selection sets we can obtain the object ids of the objects contained within. The method though, returns an array.

     

    But what if we want an ObjectIDCollection?

     

    We can simply pass the ObjectID[] array into the ObjectIDCollection constructor.

     

    Simple. The last thing you want to do is iterate through the array and add it to a collection individually. A simple yet handy hint which can save you a lot of effort.

  • Program to supersede and manage hundreds of drawings received via transmittal

    What is the problem?

    • Superseding documents is a real pain – a great difficulty. Manually clicking and deleting files: it’s very time consuming and error prone. What if you miss an important drawing? It’s very, very easy when you get 20-30 new drawings every two days to sort through. In 3 months you’ll have a couple of hundred drawings which have to be reviewed. Who’s gonna pay for that?

    Why is it a problem?

    • Essentially it’s because people want to transfer any risk associated with a project cocking up onto you. So they’ll send you, say, 700 drawings all at once. Multiple revisions of the same document. And they’ll get you to sort it all out.

    What is the solution?

    • I wrote a program which takes care of everything. Saves you time and a lot of headaches. It’s literally amazing.

    What does it do?

    I’m using terms you may not be familiar with it, so first please take a moment to understand it: Definitions: (i) current project folder –  this is the folder on your computer where you keep only the latest drawings for a particular project. (ii) Downloaded drawings folder – this is the folder where you download any “new” drawings received from a transmittal.

                    What does the program do?

    • Automatically updates your current project directory with the latest revisions which you have downloaded.
    • Cleans your downloaded drawings folder, and your current project folder, of any obsolete drawings.
    • Identifies which drawings are new and which ones are to be superseded.

    So what’s the point of all this?

    It allows our guys to focus on what they do best: getting good drawings to you. In other words, you’ll get more value for your dollar with us, because we don’t need to spend a lot of time sifting and updating drawings.

    Demo:

    Please see below for a demo. Programming this had its challenges. I chose VBA because it allows users to see and tweak what is going on. This might have been more challenging if I used WPF to do the same project.

     

    I hope you enjoy it!

    Ben

     

  • Batch Processing AutoCAD drawings using AcCoreConsole and Script Pro 2.0

    Batch Processing Using AcCoreConsole is now here.

    What is AcCoreConsole?

    • It is a command line version of AutoCAD without a user interface.
    • It allows us to batch process AutoCAD drawings super fast.

    Example:

    • Let’s say I have 500 drawings. (This is a prime candidate for batch processing).
    • Now let’s say I have to apply a tedious and repetitive change to each of those drawings.
    • This would take a long time to do manually. You would have to employ a CAD monkey to do this. No more! You can automate everything!

    Watch the Video: It is will explain the technical stuff below:

     

    For Geeks Only:

    • If you have any questions, please feel free to ask!

    Geek-speak: How does it work? (It might not make sense to most of you guys but that’s ok – all you need to know is that you CAN automate things if you want to):

    1. Write a custom .net DLL (or ARX if you prefer).
    2. Write a script file to load this dll and to apply it to a drawing, and to add the parameters your dll requires.
    3. Apply that particular script file to all 500 of your drawings using Scrip Pro 2.0.
    4. Use AcCoreConsole to do the heavy lifting. It is light weight and fast and ideally suited to batch processing.
    5. Make yourself a cup of coffee – and by the time you are done, your files will have been updated.
    6. Check the log files to ensure everything went smoothly.
    7. Important: Ensure you DLL is in a “trusted location”.

    Here is my ScriptFile:

    (Remember when pasting that you have to add the .scr extension)

    My .net DLL:

    • Just a standard DLL which changes the attribute values of a particular block. You’ll have to get rid of the pesky reference to AcMgd but otherwise it is basically the same as the below. Kean says it better than I could ever say it.
    • Of course you will have to change the above script file to suit the DLL command name, and the DLL location, as well as the particular block name and tag and new attribute values that will be particular to your project.
    •  You can access the Document with the following code:

    Document acDoc =  Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;

    • Here is Kean’s code.

     

    If you have any questions: Go ahead and call me!

  • Writing Clear Tests

    A very rudimentary but fundamental tip: When you are testing something, it is absolutely paramount that you have a crystal clear idea in your mind of exactly what you are testing.

    If you want to ensure that there are 4 tyres on your car, before you start out from your garage, you must check for exactly that:

    • are there four tyres on my car?

    If you test for something else:

    • are there tyres on my car?

    Then you may win some of the time, but other times you will get confusing and conflicting results.  What if somebody adds 3 tyres, but omits one tyre? Obviously this car will have a lot of problems going forward (pun intended), and you will wonder why all of a sudden your tests are all passing but the car is failing?

    What is going on? And it will not be immediately obvious what is going on. Because the mind can deceive, and while the eyes see, they may yet be blind. You’ll be spending a loooooooooooot of time trying to debug that one little glitch. Save yourself the headache, especially if you’re looking at the code 6 months later.

     

    So it is a very simple, but very powerful tip:

    • be very specific and clear in your mind as to exactly what you are testing for.
  • WPF Tunnelling – Explanation by analogy

    Tunnelling – What is it?

    And no, I’m not referring to how El Chapo escaped.

     

    Refresher: what is bubbling?

    In the last post we talked about “bubbling”.

    It’s kinda like those instances where you get a parking fine. If you don’t deal with it then, you’ll get another fine. If you don’t deal with it there, you will get a notice from the county court. Still not paying? Soon the police will come knocking – and like with all things – especially lies and drugs, things keep progressing to ever increasing heights of danger and pain and turmoil. Pretty soon you could end up going to jail simply because you didn’t pay a parking fine. But if you dealt with it properly at the time – well there would be no need to spend 2 years in prison for it. (Do you think I am kidding? Guess again. Why oh why wasn’t the “event” dealt with early before it bubbled up to become more serious? An excellent question. I suppose you would have to ask what was going through the mind of the judge.

     

    Tunneling is the exact oppose of bubbling

    Nebuchadnezzar was once the top dog in all the land. Nobody could so much as breath without obtaining his express written consent. All life, energy and glory started with him. But suppose something happens, suppose someone in the privacy of his own room refused to bow down to the great emperor. What did he do? He issues an event to find the culprit.

    He has an extensive network of spies. And word travels pretty quickly so this is how they would have found the guilty culprit:

    1. The King asks his Prime minister who did it.
    2. The prime ministers ask State delegates.
    3. The state delegates ask the county delegates
    4. The county people ask local area managers.
    5. The local area managers ask every Tom, Dick and Harry on the street.
    6. And the people on the street finally identify Daniel as the guy who refuses to bow down.

    In other words, an event begins at the top and works its way down the hierarchy to the control element responsible for triggering the event.

     

    Now that was simple, wasn’t it?

  • What is a routed event?

    What is a routed event?

    This is the answer that is typical on Stack Overflow sites and other such forums:

    A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.

    It seems – with all due respect to those coders – are not applying the old programming adage:

    Always write explanations as if the guy who ends up reading it is a violent psychopath who knows where you live.

    Now such MSDN techno-speak just hurts everyones’ heads. Why can’t they just speak plain English? I’m no expert, but here it is explained simply so that you can understand:

    Explanation By Analogy: What is a routed event?

    Our judicial system employs routed events. Suppose that somebody commits a crime. This is an “event”. This event can be handled in any one of the following courts, (from the lowest court to the highest court in the land, respectively):

    1. Lower/Magistrate court
    2. County/Distruct Courts
    3. Supreme Court
    4. State/Territory Court
    5. The High Court
    6. The Privy Council
    7. and above that: The Supreme Court of the Galactic Republic

    Now, as is the case, all courts have jurisdiction over the matter. A petty theft can be tried in the High Court – if you wanted to. The High court has jurisdiction, but is that what you really want? Most cases start off in the Lower courts, and then, if required, the matter is passed onto a higher court. And if so required, that court passes on the matter to a still higher court. It can go all the way to the top.

    Or on the other hand, if you so decide: the theft can be handled at the lower court, and the matter can end there.

    Routed events work the same way. If an event happens in the lower court, you, the programmer, can so choose to have the event “bubble up” and be handled by a higher court. The matter can go as high up as you wish and can be taken care of there.

    Now that, in effect, is how routed events work. Something which starts in the Lower Court can be passed on and can be “handled” and heard in the Privy Council. And that’s it! So simple? Yet everyone tries to make it so complicated!