Creat a sketch on a new part

Creat a sketch on a new part

Anonymous
Not applicable
359 Views
1 Reply
Message 1 of 2

Creat a sketch on a new part

Anonymous
Not applicable

Hello,

i use this code to creat a new part , and a new sketch. 

But the sketch doesn(t work, i don't understand why.

 

someone can help me please?

 

Sub test()

'Créer une nouvelle piece SHOP
    Dim oApp As Inventor.Application
    Set oApp = ThisApplication
    
    oTemplatesPath = oApp.DesignProjectManager.ActiveDesignProject.TemplatesPath
    oTemplateFile = "piece SHOP.ipt"
    oTemplate = oTemplatesPath & "\" & oTemplateFile
    
    Dim oPartDoc As PartDocument
    Set oPartDoc = oApp.Documents.Add(kPartDocumentObject, oTemplate)

    
'Créer une esquisse
    ' Create a 2D sketch on the X-Y plane.
    Dim sketch1 As PlanarSketch
    Set sketch1 = oPartDoc.Sketches.Add(oPartDoc.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry
    Set tg = oApp.TransientGeometry
    
    ' Draw rectangles by center point.
    Call Sketch.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(8, 3))
End Sub
0 Likes
Accepted solutions (1)
360 Views
1 Reply
Reply (1)
Message 2 of 2

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Anonymous.  It looks like you were missing a 'link in the chain' within your lines of code.  You needed to add 'ComponentDefinition' in there between the Part and the Sketches, and between the Part and the WorkPlanes.  Then you also didn't include the '1' in the variable's name for the Sketch object at the start of the last line.  Here it the corrected code that worked in my test.:

Sub test()
'Créer une nouvelle piece SHOP
    Dim oApp As Inventor.Application
    Set oApp = ThisApplication
    
    oTemplatesPath = oApp.DesignProjectManager.ActiveDesignProject.TemplatesPath
    oTemplateFile = "piece SHOP.ipt"
    oTemplate = oTemplatesPath & "\" & oTemplateFile
    
    Dim oPartDoc As PartDocument
    Set oPartDoc = oApp.Documents.Add(kPartDocumentObject, oTemplate)

    
'Créer une esquisse
    ' Create a 2D sketch on the X-Y plane.
    Dim sketch1 As PlanarSketch
    Set sketch1 = oPartDoc.ComponentDefinition.Sketches.Add(oPartDoc.ComponentDefinition.WorkPlanes.Item(3))
    
    Dim tg As TransientGeometry
    Set tg = oApp.TransientGeometry
    
    ' Draw rectangles by center point.
    Call sketch1.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(0, 0), tg.CreatePoint2d(8, 3))
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes