API : sketching in a drawing view ?

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
Reply to
Philippe Guglielmetti
Loading thread data ...

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.

Reply to
Paul Delhanty

EditSheeeeeeeeet ! that's it! thanks a lot, Paul!

Reply to
Philippe Guglielmetti

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

Reply to
Sean Phillips

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

Reply to
rocheey

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.