10 Replies Latest reply on Apr 13, 2015 1:26 PM by Rolando Garza

    Annotation Link to Property

    Rolando Garza

      Hi

      I'm new to SW macros and am looking for a bit of help.

       

      I have an Annotation on my drawing.

      I have a property named "VPN1".

      I would like the Annotation to display the value of the property.

       

      I've seen a few examples on the forum on how an annotation can be created with a link to a property.

      But I already have the annotation, i just want to link it to the property.

       

      I don't see how I can address the annotation object.

      I was hoping to snag the object by name and just change the annoation.text = property.value; but, I can't even see how to assign a name to the annotation.

       

       

      Can anyone shed some light on how I might be able to write a macro for this linking operation?

        • Re: Annotation Link to Property
          Jim Sculley

          Questions:

           

          1.  Is the property in the model or the drawing?

          2.  Is this intended to happen in new drawings, or do you have to process a bunch of existing drawings, or both?

          3.  Does the existing annotation contain any text?  If so, is it the same text for all drawings?

           

          Jim S.

          • Re: Annotation Link to Property
            Christoph Neundorf

            Why do you need a macro for that?

            Wouldn't a block fulfil the task?

             

            Simply create a block from the annotation text $PRP:"VPN1" or $PRPSHEET:"VPN1" (depending on the location of the property - model or drawing).

             

            The procedure is the same as it is used for filling title blocks.

            http://www.aboutsolidworks.com/title_block.htm

             

            Regards,

            Chris

            • Re: Annotation Link to Property
              Christoph Neundorf

              You have to know the name of the annotation feature.

              It's type should be "Note".

               

              2012 SOLIDWORKS API Help - Get Note By Name Example (VBA)

               

              If you don't know the name of the note perhaps you can check the content of all notes to match with a specified text to get the name.

              Then you can link to a property.

                • Re: Annotation Link to Property
                  Rolando Garza

                  I was hoping to address it by name.

                  But, I don't see how I can I assign the annotation a name.

                  How is that done?

                    • Re: Annotation Link to Property
                      Christoph Neundorf

                      I don't think you can't. Maybe you can via API but then you'll have to select the annotation first.

                      I guess the best way is to use the annotation block (as library feature) like mentioned before and paste this block onto all the drawings which can be done by a macro.

                        • Re: Annotation Link to Property
                          Rolando Garza

                          I was hoping it would just be a simple change of the Note's property through the right mouse button's contextual menu.

                           

                          This little script what I'm using to get by:

                          '//Annotation must be selected before code is run

                          Dim swApp As SldWorks.SldWorks

                          Dim swModel As SldWorks.ModelDoc2

                          Dim swDrawing As SldWorks.DrawingDoc

                          Dim swSelMgr As SldWorks.SelectionMgr

                          Dim swNote As SldWorks.Note

                          Dim swAnn As SldWorks.Annotation

                           

                          Sub main()

                          Set swApp = Application.SldWorks

                          Set swModel = swApp.ActiveDoc

                          Set swSelMgr = swModel.SelectionManager

                          Set swNote = swSelMgr.GetSelectedObject6(1, 0)

                          Set swAnn = swNote.GetAnnotation

                           

                          message = "Enter annotation's name."

                          Title = "Define Annotation Name"

                          myDefinedName = InputBox(message, Title, defaultValue)

                          swAnn.SetName myDefinedName

                           

                          MsgBox "Name changed to:" & myDefinedName

                           

                          End Sub

                        • Re: Annotation Link to Property
                          Jim Sculley

                          When would you assign this name? You said you have a bunch of existing drawings you would need to process.  Clearly, none of those drawings would have the proper name assigned to the annotation.

                           

                          To make this work via the API, the annotation you want to change needs to have something unique about it that you can use to distinguish it from any other annotations.  For instance, if you can say with 100% certainty that the annotation contains no text, and that it is the only annotation that contains no text on your drawings, you can simply loop through all the annotations on the drawing, looking for the one with no text and change it when you find it.  Here's some relevant code from a little utility I wrote a while back to do something similar.  I've tweaked it to look for an empty note on the sheet and change it to show the built in SW-File Name custom property.  It will also look for any empty notes in the drawing views and change them to the SW-Last Saved By custom property:

                           

                           

                               
                           private void alterNotes()         
                          {             
                               currentDrawingDoc = swApp.ActiveDoc as DrawingDoc;             
                               SolidWorks.Interop.sldworks.View v = (SolidWorks.Interop.sldworks.View)currentDrawingDoc.GetFirstView();             
                               //handle notes on sheet first 
                               object[] annArray = v.GetAnnotations() as object[];             
                               foreach (object o in annArray)             
                               {                
                                    Annotation nextAnn = null;                 
                                    Note nextNote = null;                
                                    try                 
                                    {                    
                                          nextAnn = (Annotation)o;                     
                                         if (nextAnn.GetType() != (int)swAnnotationType_e.swNote)                     
                                         {                         
                                              continue;                     
                                         }                     
                                         nextNote = (Note)nextAnn.GetSpecificAnnotation();                     
                                         string noteText = nextNote.PropertyLinkedText;                     
                                         if (noteText.Length == 0)// this is an empty note                     
                                         {                         
                                              noteText = "$PRP:\"SW-File Name\"";                     
                                         }                                    
                                         nextNote.PropertyLinkedText = noteText;                    
                                         return;                 
                                    }                 
                                    finally                 
                                    {                     
                                         if (nextAnn != null)                     
                                         {                        
                                               Marshal.FinalReleaseComObject(nextAnn);                         
                                              nextAnn = null;                     
                                         }                    
                                          if (nextNote != null)                     
                                         {                         
                                              Marshal.FinalReleaseComObject(nextNote);                        
                                              nextNote = null;                     
                                         }                 
                                    }             
                               }                  
                               //handle notes on drawing views             
                               while ((v = (SolidWorks.Interop.sldworks.View)(v.GetNextView())) != null)             
                               {                 
                                    annArray = v.GetAnnotations() as object[];                 
                                    foreach (object o in annArray)                
                                    {                     
                                         Annotation nextAnn = null;                     
                                         Note nextNote = null;                     
                                         try                     
                                         {                         
                                              nextAnn = (Annotation)o;                         
                                              if (nextAnn.GetType() != (int)swAnnotationType_e.swNote)                         
                                              {                             
                                                   continue;                         
                                              }                         
                                              nextNote = (Note)nextAnn.GetSpecificAnnotation();                                          
                                              string noteText = nextNote.PropertyLinkedText;                         
                                              if (noteText.Length == 0) //empty note                         
                                              {                             
                                                   nextNote.PropertyLinkedText="$PRP:\"SW-Last Saved By\"";                         
                                              }                                            }                     
                                              finally                     
                                              {                         
                                                   if (nextAnn != null)                         
                                              {                            
                                                   Marshal.FinalReleaseComObject(nextAnn);                             
                                                   nextAnn = null;                         
                                              }                        
                                              if (nextNote != null)                         
                                              {                            
                                                   Marshal.FinalReleaseComObject(nextNote);                             
                                                   nextNote = null;                        
                                              }                    
                                          }                 
                                    }            
                                }         
                          }

                          Jim S.