API to add sketch to flat pattern

API to add sketch to flat pattern

Anonymous
Not applicable
1,155 Views
8 Replies
Message 1 of 9

API to add sketch to flat pattern

Anonymous
Not applicable

Anyone know the API to add a sketch to the face of a flat pattern?

 

This is where I've got to so far:

 

Dim partDoc As PartDocument = ThisApplication.ActiveDocument
Dim sheetMetalDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition
If (Not sheetMetalDef.HasFlatPattern()) Then
sheetMetalDef.Unfold()
End If
Dim flatPattern As FlatPattern = sheetMetalDef.FlatPattern
flatPattern.Edit()

0 Likes
Accepted solutions (1)
1,156 Views
8 Replies
Replies (8)
Message 2 of 9

Owner2229
Advisor
Advisor
Accepted solution

Here ya go:

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentSubType.DocumentSubTypeID <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Exit Sub
Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If Not oCD.HasFlatPattern() Then
    oCD.Unfold()
Else
    oCD.FlatPattern.Edit()
End If
Dim oFP As FlatPattern = oCD.FlatPattern
Dim oFace As Face = oFP.TopFace
Dim oSketches As PlanarSketches = oFP.Sketches
Dim oSketch As PlanarSketch = oSketches.Add(oFace, False)
oSketch.Name = "MySuperSketch"
oSketch.Edit()
'Do something in the sketch
oSketch.ExitEdit()
oFP.ExitEdit()
Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 9

Anonymous
Not applicable

Thanks Michal, this is 99% of what I need.

When I do the process manually, after I select the face, it projects the geometry automatically. Do you know if there's a way to specify this in the code?

 

0 Likes
Message 4 of 9

Anonymous
Not applicable

Never mind changing False to True worked! Thanks again

0 Likes
Message 5 of 9

Anonymous
Not applicable

If "MySuperSketch" already exists, how can I delete it first before trying to add it again?

0 Likes
Message 6 of 9

Anonymous
Not applicable

Sorry again, I should really try myself before asking. This did the trick:

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentSubType.DocumentSubTypeID <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Exit Sub
Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If Not oCD.HasFlatPattern() Then
oCD.Unfold()
Else
oCD.FlatPattern.Edit()
End If
Dim oFP As FlatPattern = oCD.FlatPattern
Dim oFace As Face = oFP.TopFace
Dim oSketches As PlanarSketches = oFP.Sketches
Dim oSketch As PlanarSketch
For Each oSketch In oSketches
If oSketch.Name = "MySuperSketch"
oSketch.Delete
End If
Next
oSketch = oSketches.Add(oFace, True)
oSketch.Name = "MySuperSketch"
oSketch.Edit()
'Do something in the sketch
oSketch.ExitEdit()
oFP.ExitEdit()

0 Likes
Message 7 of 9

Anonymous
Not applicable

I thought I would be able to extract the perimeter value from the sketches region properties. There is a profile item in the sketch but no access to RegionProperties, any idea why? 

 

oSketch.Edit()
oPerimeter = oFP.Sketches.Item(1).Profiles.Item(1).RegionProperties.Perimeter * 10

0 Likes
Message 8 of 9

Owner2229
Advisor
Advisor

Little update to the code to speed it up if there's more sketches in there. Anyway, what do you need the perimeter for?

The flat pattern is always in the first quadrant. It start's at X = 0, Y = 0 and both goes to positive numbers.

So, you can simply use (0;0) as origin and width and heigth as maximal boundary.

Anyway if you "have" to have the region, you can use this:

<Querying a sketch profile to get regions>

 

cartesian-quadrants

 

 

Dim oDoc As Document = ThisApplication.ActiveDocument
If oDoc.DocumentSubType.DocumentSubTypeID <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Exit Sub
Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
If Not oCD.HasFlatPattern() Then
    oCD.Unfold()
Else
    oCD.FlatPattern.Edit()
End If
Dim oFP As FlatPattern = oCD.FlatPattern
Dim oFace As Face = oFP.TopFace
Dim oSketches As PlanarSketches = oFP.Sketches
Try
oSketches("MySuperSketch").Delete() Catch
End Try Dim oSketch As PlanarSketch = oSketches.Add(oFace, True) oSketch.Name = "MySuperSketch" oSketch.Edit()
Dim oProfile As Profile = oSketch.Profiles.AddForSolid
Dim oRegion As RegionProperties = oProfile.RegionProperties
Dim oPerimeter As Double = oRegion.Perimeter
oSketch.ExitEdit() oFP.ExitEdit()

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 9 of 9

Anonymous
Not applicable

Thanks, just what I needed! I'm not at my computer just now so I can't confirm, but I need the total perimeter, including that of any cuts inside the outer perimeter. I think regionproperties.perimeter is the correct measurement to use in this instance.

0 Likes