API : sketching in a drawing view ?

Discussion in 'SolidWorks' started by Philippe Guglielmetti, Dec 5, 2003.

  1. I can't reach to sketch in a drawing view. Below is a macro that creates a
    view and draws 10 random lines, but they appear on the background, not in
    the desired view.
    Any idea ? (Macro recording & playback doesn't work...) Thanks!

    Option Explicit
    Dim app As SldWorks.SldWorks
    Dim doc As SldWorks.DrawingDoc

    Public Sub main()
    Set app = Application.SldWorks
    Set doc = app.NewDrawing2(swDwgTemplateA3size, "", 0, 0, 0)
    Dim l, h As Double
    l = 0.3
    h = 0.2
    Dim viewname As String
    viewname = doc.CreateViewport2(0, 0, l, h, 0, 1)
    Dim view As SldWorks.view
    Set view = doc.GetFirstView
    Call doc.SelectByID(viewname, "DRAWINGVIEW", 0, 0, 0)
    Dim sketch As SldWorks.sketch
    Set sketch = view.GetSketch
    sketch.Select (False)
    Call doc.EditSketch
    Set sketch = doc.GetActiveSketch
    Dim i As Integer
    For i = 1 To 10
    Call doc.CreateLine2(l * Rnd(), h * Rnd(), 0, l * Rnd(), h * Rnd(),
    0)
    Next i
    End Sub
     
    Philippe Guglielmetti, Dec 5, 2003
    #1
  2. Philippe,

    The following macro creates lines in the desired view:

    Dim doc As SldWorks.DrawingDoc

    Public Sub main()
    Set app = Application.SldWorks
    Set doc = app.NewDrawing2(swDwgTemplateA3size, "", 0, 0, 0)
    Dim l, h As Double
    l = 0.3
    h = 0.2
    Dim viewname As String
    viewname = doc.CreateViewport2(0, 0, l, h, 0, 1)
    Call doc.SelectByID(viewname, "DRAWINGVIEW", 0, 0, 0)
    Call doc.EditSheet
    Dim i As Integer
    For i = 1 To 10
    Call doc.CreateLine2(l * Rnd(), h * Rnd(), 0, l * Rnd(), h * Rnd(),
    0)
    Next i

    End Sub

    I used EditSheet instead of EditSketch and got rid of a few lines that
    looked like they they were there for debugging purposes.
     
    Paul Delhanty, Dec 5, 2003
    #2
  3. EditSheeeeeeeeet ! that's it! thanks a lot, Paul!
     
    Philippe Guglielmetti, Dec 5, 2003
    #3
  4. you have to get the document that the drawing view was created from.
    exp. if it was a drawing view of a part file. then retrive that parts modeldoc2.
    View::ReferencedDocument

    hopes this helps
    Sean Phillips
     
    Sean Phillips, Dec 5, 2003
    #4
  5. Philippe Guglielmetti

    rocheey Guest

    I can't reach to sketch in a drawing view. Below is a macro that creates a
    Yes, I went thru this before, myself. Apparently, while SW usually
    treats any other freshly-created object as the "current" object, when
    it comes to views, the 'current' view is the sheet itself. The fix is
    a "one-liner" ..

    Option Explicit
    Dim app As SldWorks.SldWorks
    Dim doc As SldWorks.DrawingDoc

    Public Sub main()
    Set app = Application.SldWorks
    Set doc = app.NewDrawing2(swDwgTemplateA3size, "", 0, 0, 0)
    Dim l, h As Double
    l = 0.3
    h = 0.2
    Dim viewname As String
    viewname = doc.CreateViewport2(0, 0, l, h, 0, 1)

    '''''' NOT NEEDED ''''' Dim view As SldWorks.view
    '''''' NOT NEEDED ''''' Set view = doc.GetFirstView
    '''''' NOT NEEDED '''''Call doc.SelectByID(viewname, "DRAWINGVIEW", 0,
    0, 0)
    '''''' NOT NEEDED '''''Dim sketch As SldWorks.sketch
    '''''' NOT NEEDED '''''Set sketch = view.GetSketch
    '''''' NOT NEEDED '''''sketch.Select (False)
    '''''' NOT NEEDED '''''Call doc.EditSketch
    '''''' NOT NEEDED '''''Set sketch = doc.GetActiveSketch

    success = doc.ActivateView(viewname) ''''''' THE ONE "MAGIC" LINE

    Dim i As Integer
    For i = 1 To 10
    Call doc.CreateLine2(l * Rnd(), h * Rnd(), 0, l * Rnd(), h *
    Rnd(),
    0)
    Next i
    End Sub



    also, the RND function doesnt really generate random numbers unless
    you 'seed'
    it, normally with the timer function:

    Randomize Timer ' uses milliseconds past misnight as a seed
     
    rocheey, Dec 5, 2003
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.