19 Replies Latest reply on Feb 14, 2018 2:56 PM by Sergey Kolesnik

    GetAnnotationCount

    Sergey Kolesnik

      Hi there. Can anybody tell me if there is a GetAnnotationCount method for Drawing Document, not an IVew member? IModelDocExtention member is for parts only, as it seems. It returns zero for drawing.

      Using IVeiw can return total number of annotations in the document by traversing all the views, but still there may be (and usually there are some) annotations on sheet layer, not hosted by any view.

      I have a thought about some workaround such as traversing with IView::GetFirstAnnotation3 (still what if there are no views at all?) and IAnnotation::GetNext3 while GetNext3 succeeds (not sure though).

      Any advice on situation? Or maybe there's a way to return an array of all the annotations in the drawing doc? =)

       

       

      ***

      Update:

      I've tried traversing Annotations with for loop. But it seems that GetNext3 returns pointers only for Balloons, though it should return for other type regardless since Annotation is a common class.

      There are definetely more than 3 annotations. I added third balloon and another exception was thrown after i = 2. For 2 balloons it was i = 1 and exception. Though, exception after i = 2 when it actually was to stop iterating, is a question of a special interest, why doesn't it return another types of annotations??

       

      Сообщение отредактировано: Sergey Kolesnik

        • Re: GetAnnotationCount
          Jacob Corder

          the ISheet:FirstView actually returns the Sheet as a view.  so

          2018 SOLIDWORKS API Help - GetFirstView Method (IDrawingDoc)

           

          i have not found a way to get the Sheet As a view from an inactive sheet.  the only way i can find how to do that is to activate it.  i will keep looking. i have tried

           

          casting from ISheet

          casting from IFeature.GetSpecificFeature2 (Retrieved from DDOC.GetFeatureByName(SHeetName))

          maybe someone else knows a way to get the sheet as a view from an inactive sheet.

            • Re: GetAnnotationCount
              Sergey Kolesnik

              Sheet as a View? How's that?)) It definetely says of returning a pointer to the first view in the document.

              Actually, there's another question which I'll add to the main. That is, GetNext3 is to return IAnnotation object. And Annotation is a common class for Notes, Dimensions, etc. But I actually can get only those of Balloons. Why so?

                • Re: GetAnnotationCount
                  Josh Brady

                  Read the help.  It's stated pretty clearly that GetFirstView returns the sheet.

                   

                  That is, GetNext3 is to return IAnnotation object. And Annotation is a common class for Notes, Dimensions, etc. But I actually can get only those of Balloons. Why so?

                   

                  You have not posted your code, therefore we cannot find your mistake.

                    • Re: GetAnnotationCount
                      Sergey Kolesnik

                      Clear as it is.

                       

                      CComPtr<IView> pView;

                      pDrawing->IGetFirstView(&pView);

                       

                       

                      CComPtr<IAnnotation> pAnnF;

                      CComPtr<IAnnotation> pAnnS;

                      pView->GetFirstAnnotation3(&pAnnF);

                       

                       

                       

                       

                      long anType = 0;

                      BSTR anName;

                      double XY;

                       

                       

                      pAnnF->GetType(&anType);

                      pAnnF->GetName(&anName);

                      pAnnF->IGetPosition(&XY);

                       

                       

                      std::cout << "Annotation type num: " << anType << " , Name: " << anName

                      << " , Position XY: " << XY << std::endl;

                       

                       

                       

                      for (int i = 1; i != 3; ++i)

                      {

                      pAnnF->GetNext3(&pAnnS);

                      pAnnS->GetType(&anType);

                      pAnnS->GetName(&anName);

                      pAnnS->IGetPosition(&XY);

                      std::cout << "Annotation type num: " << anType << " , Name: " << anName

                      << " , Position XY: " << XY << std::endl;

                        • Re: GetAnnotationCount
                          Jacob Corder

                          in C++ can you execute a while loop?

                           

                          because in vb you write this

                           

                          Do until PAnnF is nothing

                              Do your stuff here

                              pAnnF = PAnnF.GetNext

                          loop

                          like this

                           

                          and i am only guessing here so go easy on me.

                          do-while Statement (C++)

                           

                          CComPtr<IView> pView;

                          pDrawing->IGetFirstView(&pView);

                           

                           

                          CComPtr<IAnnotation> pAnnF;

                          CComPtr<IAnnotation> pAnnS;

                          pView->GetFirstAnnotation3(&pAnnF);

                           

                          long anType = 0;

                          BSTR anName;

                          double XY;

                           

                          do

                            {

                          pAnnS->GetType(&anType);

                          pAnnS->GetName(&anName);

                          pAnnS->IGetPosition(&XY);

                          pAnnF->GetNext3(&pAnnS);

                            } while pAnns 1= null

                          • Re: GetAnnotationCount
                            Josh Brady

                             

                             

                            for (int i = 1; i != 3; ++i)

                            {

                            pAnnF->GetNext3(&pAnnS);

                            pAnnS->GetType(&anType);

                            pAnnS->GetName(&anName);

                            pAnnS->IGetPosition(&XY);

                            std::cout << "Annotation type num: " << anType << " , Name: " << anName

                            << " , Position XY: " << XY << std::endl;

                             

                            I don't C, but looks like your problem is at the end of the loop.  It goes back to the beginning to pAnnF again to get next annotation.  During loop, you need to get next annotation from previous one.  You can't get from the first one.

                          • Re: GetAnnotationCount
                            Sergey Kolesnik

                            Now I've noticed. You're talking about GetFirstView, not IGetFirstView. There's a remark for actually returning the sheet.

                              • Re: GetAnnotationCount
                                Josh Brady

                                Sergey Kolesnik wrote:

                                 

                                Now I've noticed. You're talking about GetFirstView, not IGetFirstView. There's a remark for actually returning the sheet.

                                Help for both is identical.  You have to read the whole thing.  Be thorough.  Part of competence.

                                 

                                Please study this macro.  Again, it's VBA vs C whatever, but it returns all annotation types.

                                 

                                Dim swApp As SldWorks.SldWorks
                                Dim swDoc As SldWorks.ModelDoc2
                                Dim swExt As SldWorks.ModelDocExtension
                                Dim swDwg As SldWorks.DrawingDoc
                                Dim swSht As SldWorks.Sheet
                                Dim swView As SldWorks.View
                                Dim swAnnot As SldWorks.Annotation
                                Dim i As Long
                                
                                
                                Sub main()
                                
                                
                                Set swApp = Application.SldWorks
                                Set swDoc = swApp.ActiveDoc
                                Set swExt = swDoc.Extension
                                Set swDwg = swDoc
                                Set swSht = swDwg.GetCurrentSheet
                                Set swView = swDwg.GetFirstView
                                Debug.Print "Annots: " & swExt.GetAnnotationCount
                                While Not swView Is Nothing
                                    Debug.Print swView.Name
                                    Debug.Print "view annots: " & swView.GetAnnotationCount
                                    Set swAnnot = swView.GetFirstAnnotation3
                                    i = 1
                                    While Not swAnnot Is Nothing
                                        Debug.Print , "Annotation " & i & ":", "Type: " & swAnnot.GetType, "Name: " & swAnnot.GetName
                                        Set swAnnot = swAnnot.GetNext3
                                        i = i + 1
                                    Wend
                                    Set swView = swView.GetNextView
                                Wend
                                End Sub
                                
                                  • Re: GetAnnotationCount
                                    Sergey Kolesnik

                                    What you suggested ended up with exception, but!

                                    This way there's no exception. But while loop still gets no more than 1 iteration(( 57 annotations total (I can barely see 15-20) returned. Not to mention that its value doesn't change if I remove any from drawing.

                                      • Re: GetAnnotationCount
                                        Jacob Corder

                                        your not getting any because pAnnS has never been assigned a value before the while loop

                                         

                                        delete the variable pAnnS.

                                         

                                        change everywhere pAnnS is used to pAnnF

                                         

                                        pAnnF->GetNext3(&pAnnF);

                                         

                                        move the pAnnF->GetNext3(&pAnnF) to before std::cout

                                         

                                        while (pAnnF == s_Ok)

                                        {

                                        pAnnF->GetType(&anType);

                                        pAnnF->GetName(&AnName);

                                        pAnnF->IGetPosition(&XY);

                                        pAnnF->GetNext3(&pAnnF);

                                        std::cout

                                         

                                        }