2 Replies Latest reply on Oct 25, 2013 9:32 PM by Keith Rice

    Looking for API which aligns dimension text in sketch automatically

    Mahadev Dharme

      We create a sketch calling SolidWorks Sketch APIs and add dimension.

      For example -

          we create x-y plane using API IModelDoc2::ICreatePlaneFixed2()

          We create line (10,10,0) (20,20,0) using API ISketchManager::CreateLine()

          We add dimension constraint to line using.

          The API code called is as follows.

         

      ================================

          IModelDoc2* iModelDoc2 = 0;

          BSTR retvalstr;

          m_iSwApp->GetUserPreferenceStringValue(swDefaultTemplatePart, &retvalstr);

          m_iSwApp->INewDocument2(retvalstr, swDwgPaperA2size, 0.0, 0.0, &iModelDoc2);

          double origin[3], Xaxis[3], Yaxis[3];

          origin[0] = origin[1] = origin[2] = 0.0;

          Xaxis[0] = 0.05;

          Xaxis[1] = Xaxis[2] = 0.0;

          Yaxis[0] = Yaxis[2] = 0.0;

          Yaxis[1] = 0.05;

          IRefPlane* ouputplane = 0;

          iModelDoc2->ICreatePlaneFixed2(origin, Xaxis, Yaxis, FALSE, &ouputplane);

         

          BSTR planeName;

          iModelDoc2->IGetEntityName((IEntity*)ouputplane, &planeName);

       

          IModelDocExtension* docExt = 0;

          iModelDoc2->get_Extension(&docExt);

         

          VARIANT_BOOL  retval;

          CComBSTR sPlane(L"PLANE");

          docExt->SelectByID2(planeName, sPlane, 0, 0, 0, false, 0, NULL, swSelectOptionDefault, &retval);

          iModelDoc2->InsertSketch2(true);

          iModelDoc2->ClearSelection2(true);

       

          ISketchManager* sketchManager = 0;

          iModelDoc2->get_SketchManager(&sketchManager);

       

          ISketchSegment* outputSketch = 0;

          sketchManager->CreateLine(0.01, 0.01, 0.0, 0.04, 0.04, 0.0, &outputSketch);

       

          ISketchLine *pSketchLine = NULL;

              outputSketch->QueryInterface(IID_ISketchLine, (LPVOID *)&pSketchLine);

       

          ISketchPoint* lineStartPt = 0;

          ISketchPoint* lineEndPt = 0;

          pSketchLine->IGetStartPoint2(&lineStartPt);

          pSketchLine->IGetEndPoint2(&lineEndPt);

       

          ISelectionMgr* selMgr = 0;

          iModelDoc2->get_ISelectionManager(&selMgr);

       

          CComQIPtr<ISelectData> swSelData;

          selMgr->CreateSelectData(&swSelData);

       

          long mark = 1;

          swSelData->put_Mark(mark);

       

          VARIANT_BOOL retVal = FALSE;

          lineStartPt->Select4(TRUE, swSelData, &retVal);

          lineEndPt->Select4(TRUE, swSelData, &retVal);

       

          swUserPreferenceToggle_e userPreferenceToggle = swInputDimValOnCreate;

          m_iSwApp->SetUserPreferenceToggle(userPreferenceToggle, FALSE);

       

          IDisplayDimension* displayDim = 0;

          iModelDoc2->IAddHorizontalDimension2(0.0, 0.0, 0.0, &displayDim);    // We do not know dimension location

       

          m_iSwApp->SetUserPreferenceToggle(userPreferenceToggle, TRUE);

       

          VARIANT_BOOL deSel = FALSE;

          lineStartPt->DeSelect(&deSel);

          lineEndPt->DeSelect(&deSel);

          iModelDoc2->ClearSelection2(true);

         

      ================================

       

          As expected, the dimension label is seen at origin: Please see snapshot DimLabelAtOrigin.jpg

       

         

          My question:

          Is there any API which automatically aligns the dimension label in a sketch at appropriate location? This API should compute appropriate dimension label location internally.       By using this API, we can align the labels appropriately even if they are located at origin initially using IAddHorizontalDimension2 or IAddDimension2 etc.

        • Re: Looking for API which aligns dimension text in sketch automatically
          Artem Taturevych

          Take a look at ModelDocExtension::AlignDimensions.

          ____________________________________________________

          Regards,

          Artem Taturevych, Application Engineer at Intercad (Australia)

           

          translationXpert – add-in to translate SolidWorks models

          myIntercad an integrated tool for SolidWorks Professionals

          LinkedIn SolidWorks API Education Group

          • Re: Looking for API which aligns dimension text in sketch automatically
            Keith Rice

            Hi Mahadev,

             

            IModelDocExtension::AlignDimensions doesn't work in parts or assemblies until SolidWorks 2014 SP 1.0, per SPR 737380.

             

            Until then you will have to use IAnnotation::SetPosition to modify the dimension position after you determine the desired location based on your specific criteria. You can get the IAnnotation pointer using IDisplayDimension::GetAnnotation.

             

            For example, you could get coordinates of the sketch points that define the lines you are sketching. You will need to use ISketchPoint::X,Y,Z because the last time I checked, ISketchPoint::GetCoords doesn't work. But maybe they fixed it.

             

            If you want to determine the "bounding box" of the display dimension then use IDisplayData::GetLineAtIndex3. I have never used this with dimensions (or even in a part) but here is an example with a note that might be helpful:

             

            ========

             

            For a note with a rectangular border and a single straight leader there will be index 0-5 lines, get two opposing corners of the box border and hence the size. Call GetLineAtIndexfor 0 and 1, the other indexes do not provide any further information useful to determining the size of the box. You do not need to correct for scale if you use this API.

             

            Sample VBA code:

             

                Dim lineData

                Dim x0 As Double

                Dim x1 As Double

                Dim y0 As Double

                Dim y1 As Double

                lineData = swDispData.GetLineAtIndex3(0)

                x0 = lineData(4) * 1000#

                y0 = lineData(5) * 1000#

                y1 = lineData(8) * 1000#

                lineData = swDispData.GetLineAtIndex3(1)

                x1 = lineData(7) * 1000#

                   

                Debug.Print "From: (" & Format(x0, "0.00") & ", " & Format(y0, "0.00") & ") to (" & Format(x1, "0.00") & ", " & Format(y1, "0.00") & ")"

                Debug.Print "---> " & Format(Abs(x1 - x0), "0.00") & " x " & Format(Abs(y1 - y0), "0.00")

             

            ========

             

            Keith

            SolidWorks API Tutorials