Deleting passives from KiCAD export

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
browntoastjam
Posts: 37
Joined: Fri Aug 20, 2021 9:51 pm

Deleting passives from KiCAD export

Post by browntoastjam »

Hello All,

I would like to delete all the passives from a STEP exported by KiCAD. How do I go about doing this?
I started with this code:

Code: Select all

doc = FreeCAD.ActiveDocument
for obj in FreeCAD.ActiveDocument.Objects:
	if "R_0402" in obj.Name:
		doc.removeObject(obj.Name)
	if "C_0603" in obj.Name:
		doc.removeObject(obj.Name)
  
but when I execute it, I get a problem:

Code: Select all

<class 'ReferenceError'>: Cannot access attribute 'Name' of deleted object
Thanks.


Code: Select all

OS: Ubuntu 20.04.4 LTS (XFCE/xubuntu)
Word size of FreeCAD: 64-bit
Version: 0.20.
Build type: Release
Branch: unknown
Python 3.8.10, Qt 5.12.8, Coin 4.0.0, Vtk 7.1.1, OCC 7.5.2
Locale: English/United States (en_US)
Installed mods: 
  * Assembly4 0.12.0
  * fasteners 0.3.44
  * sheetmetal 0.2.49
  * Defeaturing
  * Lithophane
  * Curves 0.4.4
  * Behave-Dark-Colors 0.0.1
  * Assembly3 0.11.3
  * kicadStepUpMod 10.13.0
  * PieMenu
  * ProDarkThemePreferencePack 1.0.0
User avatar
Chris_G
Veteran
Posts: 2579
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Deleting passives from KiCAD export

Post by Chris_G »

Imagine you have a R_0402 object -> you delete it
Then, in :

Code: Select all

if "C_0603" in obj.Name:
you try to access the deleted object.
You simply need to replace if by elif :

Code: Select all

elif "C_0603" in obj.Name:
browntoastjam
Posts: 37
Joined: Fri Aug 20, 2021 9:51 pm

Re: Deleting passives from KiCAD export

Post by browntoastjam »

Chris_G wrote: Wed Jun 22, 2022 8:54 pm Imagine you have a R_0402 object -> you delete it
Then, in :

Code: Select all

if "C_0603" in obj.Name:
you try to access the deleted object.
You simply need to replace if by elif :

Code: Select all

elif "C_0603" in obj.Name:
I am sorry I did not realise that simple mistake.
Was finally able to solve it. This is what I have so far:

Code: Select all

doc = FreeCAD.ActiveDocument

listofobjectstodelete=[]

for obj in doc.Objects:
	name=obj.Name
	if ("R_0") in name or ("C_0") in name:
		listofobjectstodelete.append(name)

for m in listofobjectstodelete:
	doc.getObject(m).removeObjectsFromDocument()
	doc.removeObject(m)
Post Reply