Announcement

Collapse
No announcement yet.

Sharing: Set Above Text Field to Dimensions ( Annotated Python )

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Sharing: Set Above Text Field to Dimensions ( Annotated Python )

    Hello all,

    Just wanted to share a Python node from my upcoming AU London Python for Beginners class in a few weeks. You guys may find it useful!



    Code:
    [COLOR=#75715e]"""[/COLOR]
    [COLOR=#75715e]IMPORTING MANAGERS[/COLOR]
    [COLOR=#75715e]"""[/COLOR]
    __author__ = [COLOR=#e6db74]'Sol Amour - [email protected]'[/COLOR]
    __twitter__ = [COLOR=#e6db74]'@solamour'[/COLOR]
    __copyright__ = [COLOR=#e6db74]'designtech.io 2018'[/COLOR]
    __version__ = [COLOR=#e6db74]'1.0.0'[/COLOR]
    
    
    [COLOR=#75715e]# Importing Reference Modules[/COLOR]
    [COLOR=#f92672][B]import[/B][/COLOR] clr [COLOR=#75715e]# CLR ( Common Language Runtime Module )[/COLOR]
    clr.[COLOR=#ffffff][B]AddReference[/B][/COLOR]([COLOR=#e6db74]"RevitServices"[/COLOR]) [COLOR=#75715e]# Adding the RevitServices.dll special Dynamo module to deal with Revit[/COLOR]
    [COLOR=#f92672][B]import[/B][/COLOR] RevitServices [COLOR=#75715e]# Importing RevitServices[/COLOR]
    [COLOR=#f92672][B]from[/B][/COLOR] RevitServices.Persistence [COLOR=#f92672][B]import[/B][/COLOR] DocumentManager [COLOR=#75715e]# From RevitServices import the Document Manager[/COLOR]
    [COLOR=#f92672][B]from[/B][/COLOR] RevitServices.Transactions [COLOR=#f92672][B]import[/B][/COLOR] TransactionManager [COLOR=#75715e]# From RevitServices import the Transaction Manager[/COLOR]
    
    [COLOR=#75715e]# Here we give the Revit Document a nickname of 'doc' which allows us to simply call 'doc' later without having to type the long namespace name [/COLOR]
    doc = DocumentManager.Instance.CurrentDBDocument
    
    
    [COLOR=#75715e]# The input ports [/COLOR]
    dimensions = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]]) [COLOR=#75715e]# Here we 'unwrap' our Dynamo objects. Dynamo and Revit Elements are different - Dynamo has wrapped them up in order to manipulate them so for us to use them in the actual Revit Project we need to unwrap them so that we can talk to the Revit API[/COLOR]
    text = IN[[COLOR=#ae81ff]1[/COLOR]] [COLOR=#75715e]# As this input is simply text we do not need to unwrap it[/COLOR]
    
    [COLOR=#75715e]# Wrapping the body of our code inside of Transactions. This allows us to effect the Revit Project ( Document ).[/COLOR]
    TransactionManager.Instance.[COLOR=#ffffff][B]EnsureInTransaction[/B][/COLOR](doc) [COLOR=#75715e]# We have to be inside of a Transaction to manipulate the Revit Project so we use the Dynamo Specific transaction wrapper ( A special way to use the Transaction Class ) to ensure we are in a transaction. This way, if anything goes wrong inside of our script the Manager will ensure nothing breaks, that any partial changes made are reverted and that our Revit file is cleaned up before progressing[/COLOR]
    
    [COLOR=#75715e]# We generate an empty catchment list to append (add to) our results[/COLOR]
    results = []
    
    [COLOR=#75715e]# We then run a For Loop across every single element that is coming into our 'dimensions' input node[/COLOR]
    [COLOR=#f92672][B]for[/B][/COLOR] dim [COLOR=#f92672][B]in[/B][/COLOR] dimensions: [COLOR=#75715e]# For every single dimension inside of our input list called 'dimensioins', do the following...[/COLOR]
        dim.Above = [COLOR=#ffffff][B]str[/B][/COLOR](text) [COLOR=#75715e]# Set the property called 'Above' ( Which refers to the Text Field entitled 'Above' inside of the Dimensions editor ) to our chosen text from our second input port[/COLOR]
        results.[COLOR=#ffffff][B]append[/B][/COLOR](dim) [COLOR=#75715e]# Then simply append ( add ) this dimension to our empty catchment list 'results'[/COLOR]
    
    TransactionManager.Instance.[COLOR=#ffffff][B]TransactionTaskDone[/B][/COLOR]() [COLOR=#75715e]# After we have our script body finish executing, we want to close our Transaction. So we once again use the wrapper to close the Transaction using the 'TransactionTaskDone()' method[/COLOR]
    
    [COLOR=#75715e]# The output port, showcasing the Revit Documents Saved Name[/COLOR]
    OUT = results
    Results in Revit:



    It should be pretty easy to set the rest of the options using this
    Attached Files
    Last edited by amoursol; June 6, 2018, 04:56 PM.

    Sol Amour

    Architectural Explorer, Digital warrior, Affectual adventurer and Curious Human Being
    Portfolio Website @ Cargo Collective

    #2
    Sol
    from what i can see - your way adds the above text to all the dims in a dim string?
    Any way to add the Above Text to just say 4 out of 10 dim's in a dim string?
    Dwane

    Comment


      #3
      Originally posted by d.stairmand View Post
      Sol
      from what i can see - your way adds the above text to all the dims in a dim string?
      Any way to add the Above Text to just say 4 out of 10 dim's in a dim string?
      The Dimension Override tool (part of WorkFlow) that Revolution Design makes can override the entire chain or individual segments. So, in API terms that is possible, just not sure how it would be done in Dynamo.
      Revit for newbies - A starting point for RFO


      chad
      BEER: Better, Efficient, Elegant, Repeatable.

      Comment


        #4
        You can definitely do that, but it adds a bit more difficulty to it. I added a few things to @Sol's code below.

        Basically, you need to access the individual dimension segements of the multi-segmented dimension. In my case I am doing this by allowing for the user to add which indices the override should happen. (I appended -john, to the lines I added)

        Code:
        [COLOR=#75715e]"""[/COLOR]
        [COLOR=#75715e]IMPORTING MANAGERS[/COLOR]
        [COLOR=#75715e]"""[/COLOR]
        __author__ = [COLOR=#e6db74]'Sol Amour - [EMAIL="[email protected]"][email protected][/EMAIL]'[/COLOR]
        __twitter__ = [COLOR=#e6db74]'@solamour'[/COLOR]
        __copyright__ = [COLOR=#e6db74]'designtech.io 2018'[/COLOR]
        __version__ = [COLOR=#e6db74]'1.0.0'[/COLOR]
        
        
        [COLOR=#75715e]# Importing Reference Modules[/COLOR]
        [COLOR=#f92672][B]import[/B][/COLOR] clr [COLOR=#75715e]# CLR ( Common Language Runtime Module )[/COLOR]
        clr.[COLOR=#ffffff][B]AddReference[/B][/COLOR]([COLOR=#e6db74]"RevitServices"[/COLOR]) [COLOR=#75715e]# Adding the RevitServices.dll special Dynamo module to deal with Revit[/COLOR]
        [COLOR=#f92672][B]import[/B][/COLOR] RevitServices [COLOR=#75715e]# Importing RevitServices[/COLOR]
        [COLOR=#f92672][B]from[/B][/COLOR] RevitServices.Persistence [COLOR=#f92672][B]import[/B][/COLOR] DocumentManager [COLOR=#75715e]# From RevitServices import the Document Manager[/COLOR]
        [COLOR=#f92672][B]from[/B][/COLOR] RevitServices.Transactions [COLOR=#f92672][B]import[/B][/COLOR] TransactionManager [COLOR=#75715e]# From RevitServices import the Transaction Manager[/COLOR]
        
        [COLOR=#75715e]# Here we give the Revit Document a nickname of 'doc' which allows us to simply call 'doc' later without having to type the long namespace name [/COLOR]
        doc = DocumentManager.Instance.CurrentDBDocument
        
        
        [COLOR=#75715e]# The input ports [/COLOR]
        dimensions = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]]) [COLOR=#75715e]# Here we 'unwrap' our Dynamo objects. Dynamo and Revit Elements are different - Dynamo has wrapped them up in order to manipulate them so for us to use them in the actual Revit Project we need to unwrap them so that we can talk to the Revit API[/COLOR]
        text = IN[[COLOR=#ae81ff]1[/COLOR]] [COLOR=#75715e]# As this input is simply text we do not need to unwrap it[/COLOR]
        
        indices = IN[[COLOR=#ae81ff]2[/COLOR]] [COLOR=#75715e]# allows us to specify which segment to add the value to - john[/COLOR]
        
        [COLOR=#75715e]# Wrapping the body of our code inside of Transactions. This allows us to effect the Revit Project ( Document ).[/COLOR]
        TransactionManager.Instance.[COLOR=#ffffff][B]EnsureInTransaction[/B][/COLOR](doc) [COLOR=#75715e]# We have to be inside of a Transaction to manipulate the Revit Project so we use the Dynamo Specific transaction wrapper ( A special way to use the Transaction Class ) to ensure we are in a transaction. This way, if anything goes wrong inside of our script the Manager will ensure nothing breaks, that any partial changes made are reverted and that our Revit file is cleaned up before progressing[/COLOR]
        
        [COLOR=#75715e]# We generate an empty catchment list to append (add to) our results[/COLOR]
        results = []
        
        [COLOR=#75715e]# We then run a For Loop across every single element that is coming into our 'dimensions' input node[/COLOR]
        [COLOR=#f92672][B]for[/B][/COLOR] dim [COLOR=#f92672][B]in[/B][/COLOR] dimensions: [COLOR=#75715e]# For every single dimension inside of our input list called 'dimensions', do the following...[/COLOR]
            [COLOR=#f92672][B]for[/B][/COLOR] i [COLOR=#f92672][B]in[/B][/COLOR] indices: [COLOR=#75715e]# this portion allows you to select the individual pieces of dimension - john[/COLOR]
        dim.Segments[i].Above = [COLOR=#ffffff][B]str[/B][/COLOR](text) [COLOR=#75715e]# Set the property called 'Above' ( Which refers to the Text Field entitled 'Above' inside of the Dimensions editor ) to our chosen text from our second input port[/COLOR]
        results.[COLOR=#ffffff][B]append[/B][/COLOR](dim) [COLOR=#75715e]# Then simply append ( add ) this dimension to our empty catchment list 'results'[/COLOR]
            
            
        
        TransactionManager.Instance.[COLOR=#ffffff][B]TransactionTaskDone[/B][/COLOR]() [COLOR=#75715e]# After we have our script body finish executing, we want to close our Transaction. So we once again use the wrapper to close the Transaction using the 'TransactionTaskDone()' method[/COLOR]
        
        [COLOR=#75715e]# The output port, showcasing the Revit Documents Saved Name[/COLOR]
        OUT = results
        And the result
        Attached Files
        Last edited by johnp; June 7, 2018, 01:57 PM.

        John Pierson
        Creator of Rhythm, Monocle, Bang! and Lots of Other Okay Stuff.
        Twitter - @60secondrevit | Email - [email protected] | Work - parallaxteam.com | Blog - designtechunraveled.com | 🔗- bio.link/johnpierson

        Comment


          #5
          Originally posted by d.stairmand View Post
          Sol
          from what i can see - your way adds the above text to all the dims in a dim string?
          Any way to add the Above Text to just say 4 out of 10 dim's in a dim string?
          Totally possible as per John's reply above - mine was to be a simple (First point of contact) look at the Revit API through Python for an AU Class

          As a side note, I should have pushed this into my post as well for the winky-face...



          Code:
          [COLOR=#f92672][B]if[/B][/COLOR] [COLOR=#ffffff][B]isinstance[/B][/COLOR]( IN[[COLOR=#ae81ff]0[/COLOR]], list):
              ele = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]])[[COLOR=#ae81ff]0[/COLOR]]
          [COLOR=#f92672][B]else[/B][/COLOR]:
              ele = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]])
          
          OUT = [COLOR=#ffffff][B]dir[/B][/COLOR](ele)
          This allows you to look at stuff you select and 'Snoop' it - as you can see in the Watch node there is a 'Segments' bit.

          To look at this elements segements, simply change the Python to this

          Code:
          [COLOR=#f92672][B]if[/B][/COLOR] [COLOR=#ffffff][B]isinstance[/B][/COLOR]( IN[[COLOR=#ae81ff]0[/COLOR]], list):
              ele = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]])[[COLOR=#ae81ff]0[/COLOR]]
          [COLOR=#f92672][B]else[/B][/COLOR]:
              ele = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]])
          
          OUT = [COLOR=#ffffff][B]dir[/B][/COLOR](ele.Segments)
          And you will see that you can do stuff to the Segments too!
          Attached Files
          Last edited by amoursol; June 7, 2018, 05:40 PM.

          Sol Amour

          Architectural Explorer, Digital warrior, Affectual adventurer and Curious Human Being
          Portfolio Website @ Cargo Collective

          Comment


            #6
            Originally posted by amoursol View Post
            Totally possible as per John's reply above - mine was to be a simple (First point of contact) look at the Revit API through Python for an AU Class
            For sure! I would definitely not consider dealing with multi-segment dimensions a beginner thing. They are a pain to deal with.

            John Pierson
            Creator of Rhythm, Monocle, Bang! and Lots of Other Okay Stuff.
            Twitter - @60secondrevit | Email - [email protected] | Work - parallaxteam.com | Blog - designtechunraveled.com | 🔗- bio.link/johnpierson

            Comment


              #7
              Originally posted by johnp View Post
              For sure! I would definitely not consider dealing with multi-segment dimensions a beginner thing. They are a pain to deal with.
              As a side note, this did provide the perfect opportunity to showcase error catching So I amended my code (Thanks for the inspiration @johnp and query @d.stairmand) to capture both cases.

              Code:
              [COLOR=#75715e]"""[/COLOR]
              [COLOR=#75715e]IMPORTING MANAGERS[/COLOR]
              [COLOR=#75715e]"""[/COLOR]
              __author__ = [COLOR=#e6db74]'Sol Amour - [email protected]'[/COLOR]
              __twitter__ = [COLOR=#e6db74]'@solamour'[/COLOR]
              __copyright__ = [COLOR=#e6db74]'designtech.io 2018'[/COLOR]
              __version__ = [COLOR=#e6db74]'1.0.0'[/COLOR]
              
              
              [COLOR=#75715e]# Importing Reference Modules[/COLOR]
              [COLOR=#f92672][B]import[/B][/COLOR] clr [COLOR=#75715e]# CLR ( Common Language Runtime Module )[/COLOR]
              clr.[COLOR=#ffffff][B]AddReference[/B][/COLOR]([COLOR=#e6db74]"RevitServices"[/COLOR]) [COLOR=#75715e]# Adding the RevitServices.dll special Dynamo module to deal with Revit[/COLOR]
              [COLOR=#f92672][B]import[/B][/COLOR] RevitServices [COLOR=#75715e]# Importing RevitServices[/COLOR]
              [COLOR=#f92672][B]from[/B][/COLOR] RevitServices.Persistence [COLOR=#f92672][B]import[/B][/COLOR] DocumentManager [COLOR=#75715e]# From RevitServices import the Document Manager[/COLOR]
              [COLOR=#f92672][B]from[/B][/COLOR] RevitServices.Transactions [COLOR=#f92672][B]import[/B][/COLOR] TransactionManager [COLOR=#75715e]# From RevitServices import the Transaction Manager[/COLOR]
              
              [COLOR=#75715e]# Here we give the Revit Document a nickname of 'doc' which allows us to simply call 'doc' later without having to type the long namespace name [/COLOR]
              doc = DocumentManager.Instance.CurrentDBDocument
              
              
              [COLOR=#75715e]# The input ports [/COLOR]
              dimensions = [COLOR=#ffffff][B]UnwrapElement[/B][/COLOR](IN[[COLOR=#ae81ff]0[/COLOR]]) [COLOR=#75715e]# Here we 'unwrap' our Dynamo objects. Dynamo and Revit Elements are different - Dynamo has wrapped them up in order to manipulate them so for us to use them in the actual Revit Project we need to unwrap them so that we can talk to the Revit API[/COLOR]
              text = IN[[COLOR=#ae81ff]1[/COLOR]] [COLOR=#75715e]# As this input is simply text we do not need to unwrap it[/COLOR]
              
              [COLOR=#75715e]# Wrapping the body of our code inside of Transactions. This allows us to effect the Revit Project ( Document ).[/COLOR]
              TransactionManager.Instance.[COLOR=#ffffff][B]EnsureInTransaction[/B][/COLOR](doc) [COLOR=#75715e]# We have to be inside of a Transaction to manipulate the Revit Project so we use the Dynamo Specific transaction wrapper ( A special way to use the Transaction Class ) to ensure we are in a transaction. This way, if anything goes wrong inside of our script the Manager will ensure nothing breaks, that any partial changes made are reverted and that our Revit file is cleaned up before progressing[/COLOR]
              
              [COLOR=#75715e]# We generate an empty catchment list to append (add to) our results[/COLOR]
              results = []
              
              [COLOR=#75715e]# We then run a For Loop across every single element that is coming into our 'dimensions' input node[/COLOR]
              [COLOR=#f92672][B]for[/B][/COLOR] dim [COLOR=#f92672][B]in[/B][/COLOR] dimensions: [COLOR=#75715e]# For every single dimension inside of our input list called 'dimensioins', do the following...[/COLOR]
                  numOfSegs = dim.NumberOfSegments [COLOR=#75715e]# Count the amount of Segments the Dimensions has[/COLOR]
                  [COLOR=#f92672][B]if[/B][/COLOR] numOfSegs > [COLOR=#ae81ff]0[/COLOR]: [COLOR=#75715e]# Run an 'If' conditional check to see if it has more than zero (Which in turn means it's Segmented). If the answer to this check is True (It's either True or False) then do the following...[/COLOR]
                      [COLOR=#f92672][B]for[/B][/COLOR] num [COLOR=#f92672][B]in[/B][/COLOR] [COLOR=#ffffff][B]range[/B][/COLOR](numOfSegs): [COLOR=#75715e]# Create a Number Range starting at zero and ranging to the total count of our Number of Segments[/COLOR]
                          dim.Segments[num].Above = [COLOR=#ffffff][B]str[/B][/COLOR](text) [COLOR=#75715e]# Then for every segmented Dimension, change each Segments 'Above' property ( Which refers to the Text Field entitled 'Above inside of the Dimensions editor ), at each index ( Supplied by our variable num ), to our chosen Text value from our Input port[/COLOR]
                          results.[COLOR=#ffffff][B]append[/B][/COLOR](dim) [COLOR=#75715e]# Then simply append ( add ) this dimension to our empty catchment list 'results'[/COLOR]
                  [COLOR=#f92672][B]else[/B][/COLOR]:
                      dim.Above = [COLOR=#ffffff][B]str[/B][/COLOR](text) [COLOR=#75715e]# Set the property called 'Above' ( Which refers to the Text Field entitled 'Above' inside of the Dimensions editor ) to our chosen text from our second input port[/COLOR]
                      results.[COLOR=#ffffff][B]append[/B][/COLOR](dim) [COLOR=#75715e]# Then simply append ( add ) this dimension to our empty catchment list 'results'[/COLOR]
              
              TransactionManager.Instance.[COLOR=#ffffff][B]TransactionTaskDone[/B][/COLOR]() [COLOR=#75715e]# After we have our script body finish executing, we want to close our Transaction. So we once again use the wrapper to close the Transaction using the 'TransactionTaskDone()' method[/COLOR]
              
              [COLOR=#75715e]# The output port, showcasing the Revit Documents Saved Name[/COLOR]
              OUT = results

              Sol Amour

              Architectural Explorer, Digital warrior, Affectual adventurer and Curious Human Being
              Portfolio Website @ Cargo Collective

              Comment

              Related Topics

              Collapse

              Working...
              X