7 Replies Latest reply on Nov 28, 2014 7:40 PM by Yong Ning

    Sketch pattern with 4 points with dimension from the edges

    roberto gennari

      Hallo,

      I have an idea I'd like to develop, but I do not know the right strategy, I would like to share with you

      The initial condition is a sketch in an area with 4 edges

      Run the macro and 4 points are created automatically with dimension from the edges, as in Figure and

      as in the attached part.

      At the end of the macro I can change some of the dimensions independently manually

       

      Currently they are able to create the point in the right position,

      creating an offset of the edges of the face, then get the coordinates of the start and end point of the segments and create points here.

      Subsequently delete the segments, but do not know how to dimension the points by its edges and place dimensions near the point

       

      What do you think

       

      Thanks to all

        • Re: Sketch pattern with 4 points with dimension from the edges
          roberto gennari

          I wrote a first variant of the code VB.net for my application, for the insertion of a single point at a time:

          Prerequisites:

          - A solid part of which go to edit the sketch of the points of the holes

           

          the application uses swPart_SketchSolveNotify to intercept the insertion point in a WizardHole.

           

          I have two problems and I do not understand how to solve them:

           

          The first:

          After inserting the point I'm going to enter the dimensions but the program crashes as shown.

          I do not understand where the problem, because if I remove the code for the dimension everything works.

           

          The second:

          how can I find the right positioning coordinates of dimensions close to the point as in the previous figure of Annex?

           

          I enclose the part I'm working on the code and then insert that into a Vb.net Addin.

           

          Thank you all for any replies

           

          Roberto

            • Re: Sketch pattern with 4 points with dimension from the edges
              Artem Taturevych

              Hi Roberto,

               

              Use OnIdle notification which comes after SketchSolve notifiy to safely add the dimension.

              Can you please clarify your second question because I do not understand it?

                • Re: Sketch pattern with 4 points with dimension from the edges
                  roberto gennari

                  Hi Artem,

                  excuse me for my bed english,

                  thanks for your answer, now i try the program.

                  The second problem concerns the positioning coordinates to the text dimension.

                  I would like to place them in an orderly manner, as shown in the picture (near the point and near the edge).

                   

                  Thanks

                    • Re: Sketch pattern with 4 points with dimension from the edges
                      roberto gennari

                      Hello,

                      I added the event OnIdleNotify and after I added the code of the dimensions in this, but it does not work ...

                      At the end of the code continues to crash

                      where I am continuing to make mistakes?

                       

                      Thanks

                       

                      AddHandler msrcSldWorks.OnIdleNotify, AddressOf msrcSldWorks_OnIdleNotify

                       

                      This is what I wrote in the event

                       

                       

                          Private Function msrcSldWorks_OnIdleNotify() As Long

                       

                       

                              If isok = 0 Then
                                  Exit Function
                              End If

                              iSwApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate, False)

                              Dim boolstatus As Boolean
                              Dim boolstatus1 As Boolean
                              'Dim swSketchMgr As SelectionMgr
                              'swSketchMgr = swModel.SketchManager

                              'swSketchMgr.Insert3DSketch(True)
                              'swSketchMgr.AddToDB = True

                              Dim swDisp As DisplayDimension
                              Dim swDim As Dimension
                              esciFunzione = 1
                              Dim myswModel As ModelDoc2
                              myswModel = swModel
                              swDisp = myswModel.AddDimension2(0.01, 0.01, 0)
                              swDim = swDisp.GetDimension
                              'Debug.Print(swDim.FullName, swDim.SetSystemValue, swDim.Value)
                              swModel.ClearSelection2(True)

                              boolstatus1 = vEdges(CInt(Valori1(1).ToString)).Select4(True, swSelData)
                              boolstatus = swModel.Extension.SelectByID2("Point" & CStr(MyLastPoint), "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

                              swDisp = myswModel.AddDimension2(0.01, 0.01, 0)
                              swDim = swDisp.GetDimension
                              'Debug.Print(swDim.FullName, swDim.SetSystemValue, swDim.Value)
                              swModel.ClearSelection2(True)

                              isok = 0

                          End Function

                       

                       

                       

                       

                          Private Function swPart_SketchSolveNotify(ByVal featName As String) As Long
                              Try
                                  Dim vSkPtArr As Object
                                  Dim vSkPt As Object
                                  Dim swSkPt As SketchPoint
                                  vSkPtArr = MyFeature.GetSketchPoints2 : If IsNothing(vSkPtArr) Then Exit Function

                                  For Each vSkPt In vSkPtArr
                                      swSkPt = vSkPt
                                      ProcessSketchPoint(iSwApp, swModel, swSkPt)
                                  Next vSkPt
                                  Debug.Print("ok")

                                  If OldMyLastPoint = MyLastPoint Then Exit Function ' se non è stato inserito un punto esco

                                  OldMyLastPoint = MyLastPoint

                                  swModel.ClearSelection2(True)
                                  Debug.Print(MyLastPoint)
                                  Dim boolstatus As Boolean
                                  swModel.ClearSelection2(True)
                                  '
                                  boolstatus = swModel.Extension.SelectByID2("Point" & CStr(MyLastPoint), "SKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
                                  '
                                  If boolstatus = True Then
                                      swModel.ClearSelection2(True)
                                      Dim pFeat As Feature
                                      Dim pSketch As Sketch
                                      Dim pEntity As Entity
                                      Dim nEntType As Long
                                      Dim pFace As Face2
                                      Dim vNormal As Object

                                      Dim bRet As Boolean
                                      '
                                      pSketch = MyFeature.GetSpecificFeature2
                                      pEntity = pSketch.GetReferenceEntity(nEntType)
                                      '
                                      If nEntType = 2 Then 'se lo schizzo è appoggiato in una una faccia
                                          pFace = pEntity
                                          vNormal = pFace.Normal
                                          '
                                          Debug.Print("Face Selected!")
                                          Debug.Print("  Normal   = (" + Str(vNormal(0)) + "," + Str(vNormal(1)) + "," + Str(vNormal(2)) + ")")
                                          'bRet = pEntity.Select2(False, 0)

                       

                                          Dim boolstatus1 As Boolean
                                          Dim nDist As Double
                                          Dim vPt1 As Object
                                          Dim vPt2 As Object
                                          Dim swSelObj1 As Object
                                          Dim swSelObj2 As Object


                                          NbrOfEdges = pFace.GetEdgeCount

                                          Debug.Print("Number of edges on this face: " & NbrOfEdges)
                                          ' Get the edges on the selected face
                                          vEdges = pFace.GetEdges

                                          ' Show each edge on the face
                                          swSelData = swSelMgr.CreateSelectData

                                          For i = 0 To (NbrOfEdges - 1)
                                              boolstatus1 = vEdges(i).Select4(True, swSelData)
                                              boolstatus = swModel.Extension.SelectByID2("Point" & CStr(MyLastPoint), "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

                                              swSelObj1 = swSelMgr.GetSelectedObject6(1, 0)
                                              swSelObj2 = swSelMgr.GetSelectedObject6(2, 0)

                                              'Print("SelCount = " & swSelMgr.GetSelectedObjectCount)

                                              nDist = swModel.ClosestDistance(swSelObj1, swSelObj2, vPt1, vPt2)

                                              Debug.Assert(nDist > 0.0#)
                                              mystring = CStr(nDist) & "#" & CStr(i)
                                              myarr(i) = mystring
                                              swModel.ClearSelection2(True)
                                          Next i

                                          System.Array.Sort(Of String)(myarr)
                                          Debug.Print(myarr.GetValue(0).ToString)
                                          Debug.Print(myarr.GetValue(1).ToString)
                                          Debug.Print(myarr.GetValue(2).ToString)
                                          Debug.Print(myarr.GetValue(3).ToString)

                                          Valori = (myarr.GetValue(0)).Split(New Char() {"#"}) 'CREO I CAMPI TAGLIANDO LA RIGA COMPLETA
                                          Valori1 = (myarr.GetValue(1)).Split(New Char() {"#"}) 'CREO I CAMPI TAGLIANDO LA RIGA COMPLETA

                                          boolstatus1 = vEdges(CInt(Valori(1).ToString)).Select4(True, swSelData)
                                          boolstatus = swModel.Extension.SelectByID2("Point" & CStr(MyLastPoint), "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

                                          isok = 1

                                      End If

                                  Else
                                  End If
                              Catch ex As Exception

                              End Try

                              msrcSldWorks_OnIdleNotify()


                          End Function